Introduction to Django & Prerequisites
Hey guys, welcome to this brand new series on the Django web framework for Python. So the Django web framework allows you to use Python in order to build your website. So it's a very, very powerful framework and I think the best way to learn it is probably just to get straight in and start with a project.
I'm going to assume that you have a basic understanding of Python or you've seen my Python for beginners tutorial series already prior to watching this video series, because you are going to need to know a fair bit of basic Python. So that you don't get confused when I say for-loops or while-loops or variables or strings or classes or anything like that, because you might see quite a lot of that stuff. And if you don't know what a function is, then you might get slightly lost or it might take you a little bit longer or you might have to rewatch some of these videos. So that said, let's just go ahead and get started.
Terminal and Environment Setup
So you can see on my screen here, I'm just going to open up iTerm, which is what I use to use my terminal on a Mac. And I'm just going to exit full screen so you can see it a little better. And this is just the same as if I was to type 'terminal'. So Terminal is what's installed on a Mac by default, but then I prefer to use iTerm2 just because it's a much better program for running the terminal, although they do do the same thing. So if you just fancy using the terminal, by all means go ahead and use that, but I'm just saying that's what I use.
Creating Your First Django Project
So now that we've got that out of the way, the next thing we're going to do is really start our Django project. So to do that, I'm first going to change into the folder that I want to create the project in because when we create this Django project, it's going to create a file, a folder and file structure. So we're going to want to be in the right place before it just puts that somewhere. Otherwise, we won't know where it put it or, you know, if we're in the Documents folder, we don't want that project directly in our Documents folder, then that it'll be in the wrong place and we'll have to move it, which is a bit of a pain. So, well, that's one, it's one more command anyway. So I'm just going to change into my Django folder, which is where I want to keep this particular Django project.
And if you want to see any of the files and folders in this current directory, you can do ls and that would just say, okay, get me all the names of the folders and files within this current directory, so my Django folder. So I already have a Django project called 'test' and I'm just going to do django-admin startproject tutorial. So what that's done is it's started all the, it's given me all the files and folders that I need to be able to create my Django project. So that is the folder that contains the entire project. So we can change into that folder.
Installing Django and Choosing a Text Editor
And then I'm just going to open it in my text editor. You can use Vim or you can use Sublime Text or PyCharm or even the default editor you get with your Python installation off python.org, although I don't recommend it because it's not very good. It's not very fully featured. But by all means, use what you're comfortable with. So I use Atom personally, but feel free to use anything else.
So if you can't run django-admin startproject, it might be because you don't have Python or Django installed. If you haven't got Python installed, you can see my video that I did on that, but otherwise, I'm going to go ahead and show you how to install Django because you might not have that installed yet either. So if you're using a virtual environment, then you won't want to do sudo, but in this case, because I'm not, you're going to need to do that and you're going to need the administrator password. So if you don't have administrator access, then it's going to be difficult to install things using pip. But I'm going to do sudo pip install Django. So what that's going to do, well, it's going to ask for your password because sudo means super user do, so it needs that administrative privilege. But then what it's going to do is just install Django for you if you haven't got it. So I just typed my password and you can see here it says requirement already satisfied because I've already got it installed, but hopefully for you that should install it fine.
Exploring the Django Project File Structure
So I've just opened up this 'tutorial' folder in Atom, which is my text editor of choice, and you can see we've got a couple of different things inside this main tutorial folder. So we've got the folder again, which is the same name as the main project folder, and we've got a few files inside that. So we've got manage.py, which we're going to use in a minute. And we've got wsgi. So this is the file where your application will start when you're in a production environment. So on the web server, this is where your web server is going to look first to be able to decide what it needs to do with your Django application, and this is just saying, look at the settings file, essentially. So we can look at the settings file. So there's lots of stuff in here which you don't really need to know just yet, except for maybe the installed apps, which we're going to get to shortly as soon as we start to build out this application. We're going to need to use the installed apps, or at least add something to this Python list here. And that's pretty much all you need to know on the settings file. You can read these comments because Django is very helpful with giving you links to the appropriate documentation where necessary and everything like that. It even showed us how we started this project in case you forget that command. So it's right there. And also the version of Django that I'm using. So lots of useful information.
But the next thing it's going to look at is the URLs. So this is really saying anything that's in that URL bar at the top of your browser is defined in this file. So right now we've just got one for admin. And this is the default Django admin, which is a whole separate video in itself, if not more. So it's very, very powerful in other words.
Running the Development Server
So let's go ahead and run this project to see what happens if we just run the default project. Because, you know, is it even going to work? So let's do python manage.py, not man, manage.py runserver. So runserver is a Django command that just says run the local Django development server on your computer. So you can pretty much preview the code that you've written before you then deploy it on your live server which is going to be accessible by the internet. So let's go and run that server.
And this is what the Django server looks like. You you're going to get quite familiar with this over the next few tutorials because this is how we are going to be able to test our code. So to just explain what this is, you can ignore the red for now because it just means we haven't set up our database properly. You don't have to worry about that, because we're not really using a database at the moment. All we've got is the basic template. So it also says what version of Django we're using, and also where our settings file is. So if you remember, I noticed, we talked about the settings file. So this is in the main project root and then it's in the tutorial folder and then in the settings.py. So that's where it's looking for its settings. And it says starting development server at this. So if we go to to this address in our web browser, I'm just using Google Chrome here, and we can see if everything works, that we get the default Django template. So this is the final page that pretty much says our project is up and running and we can now continue to work on it because this is what the basic template looks like. And it really gives you a foundation for you to be able to completely customize and build your own website.
The Django Admin Interface & Next Steps
And one more thing, I thought I might as well show you the URLs that we were talking about and how it defines all the URLs in the web browser. So it's got admin in it at the moment. So if I do /admin, that gets us to another web page which is also there by default. And this is a very, very powerful one, which I'll be able to talk about quite a bit in future videos, or be a bit later on.
So that's a basic introduction to the Django web framework for Python. In the next one, we're going to actually start to write some Python code and customize our basic template that we have at the moment. So I think we're going to change this homepage and make it unique, make it our own.