Join Caleb Smith for an in-depth discussion in this video Creating a Django project, part of Learning Python and Django (2015).
Now that python, pip and Django are installed, let's get started creating our first project. First, we'll find a location to place the folder for the files of our project. This can be on the desktop, or anywhere else that you can find easily. For this video, I will work out of the Exercise Files, which I've placed on the desktop. Next, we'll open a terminal window and change the directory to the folder we've chosen. For me this will be inside of the Exercise Files Subfolders.
Once we're inside the folder we want the project in, we run a command that will create a folder in a set of files. This command is django-admin startproject firstdjango all one word, lower case. By calling our project firstdjango, this command will create a folder called firstdjango. Next, I'll use Finder to look inside of this folder.
From here, I see a file called manage.py, and another folder inside of it called firstdjango. Navigating inside of that, we see __init__.py file that we call dunder init for short. There's also a settings.py file, a urls.py and a wsgi.py. Each of these are auto-generated from the start project command that we just ran. The manage.py file runs commands associated with our project.
Now, I'll clear the terminal with control + l so that it's more clear as we move on. Now, I'll navigate inside of our project called firstdjango using cd firstdjango and hit enter. When I type ls, I see that manage.py file is there. We run python manage.py to see a list of the available commands. To use these subcommands, we type python manage.py space and then the name of that subcommand. As an example, we'll run the runserver, all one word, lower case subcommand, which provides a local web server for the developement of our Django project.
I'll type enter to run this. We'll read the output to ensure it's working. It should end with a message about our Django version, the settings file being used, followed by the IP address and port number that's it's available in from a web browser. The default port is 8000. This message also informs us that we can quit the server using Control-C. You'll also see that there's an error message in red where it says unapplied migrations, which will will address later. To see that our server is working, we open a web browser and navigate to localhost:8000.
Now, we'll see that the page says: It worked! Confratulations on your first Django-powered page. Well, we haven't accomplished much, yet, but this confirms that our project is currently working. Let's look briefly at the role of each of these files. We'll ignore the dunder init file because it will remain blank. It was generated so that python will consider the folder to be a python module. The role of the manage.py file is to run commands for our project, such as the runserver, as we've seen.
Inside of the first Django folder are the files wsgi.py, settings.py and urls.py. The wsgi.py file is used by a web server to run the Django project. The settings file is used to configure our project, and the urls file configures what code is run when a request comes in based on the url path, which we will see later. While the manage.py and wsgi.py files are important, they are mostly left alone after being auto-generated, as well as the dunder init file.
We do edit the settings.py and urls.py files to configure our project, and, in the case of urls.py, to add routes within our web application as new features are added.
- Creating a new Django project
- Defining Django models and fields
- Querying data with the Django ORM
- Building views and templates
Skill Level Intermediate
Q: pip says it was installed correctly, and I've updated the PATH variable as shown, but the pip --version command isn't working. Why?
A: Depending on your computer's setup, you'll need to restart your terminal/command prompt before the additions you've made to the PATH variable are respected. With some setups, even that won't be enough, and you may need to restart the entire machine before you are able to use the pip command directly.