Django is a popular web framework built on top of Python that allows us to create maintainable web applications quickly with less code.
What Features Django Comes with?
Django comes with a collection of tools used to build websites. Some of the tools that Django comes with are object-relational mapper or ORM, which helps us to make database queries without actually writing SQL statements.
It also comes with URL routing, which determines what logic to follow depending on the URL of a web request. Another feature is the HTML templating which allows us to have presentation logic and insert data into our HTML. And some other features like form handling, unit testing, user authentication, and so on.
In this article, I’m going to show you how to install Django 3 in a virtual environment and create your first Django project.
To start developing with Django 3, we first need to install Python 3.6 or higher. Suppose you want to check Python 3 installation. You can run
python3 --version on macOS or Linux or
python --version on Windows. And the Terminal will tell you the installed version of the Python if installed.
Create a Python 3 Virtual Environment
Next, we will need to navigate to the directory that we want to put our Django project into it. I’m then going to create a virtual environment. I will be using “pipenv” to generate the virtual environment and for installing Python packages. Run
pipenv --three to create a Python 3 virtual environment.
Install Django 3
Now, I want to install Django 3.2.3, which is the latest long-term support version of Django. Run
pipenv install django==3.2.3, and it will install Django 3 in the virtual environment and add Django to our project dependencies.
Create a Django 3 Project
Then we will need to activate the virtual environment we just created by running “pipenv shell” in the Terminal. To create a Django project in the activated virtual environment, run
django-admin startproject and then the name of our project followed by the directory name to put that project into it. I will use “mysite” as the project name, and I will use dot to create that project in the current directory. And it will generate the initial set of files for our Django 3 project.
Django Project Structure
Now, I will open that folder in VSCode, and we can see the files that the script generated for us.
We use the “manage.py” file to run various commands for our project.
The “init.py” or dunder init file tells Python that this folder contains Python files.
The “WSGI” or “wuzgi” file provides a hook for web servers such as Apache or Nginx.
The “asgi.py” file is similar to WSGI but for asynchronous web servers.
The “settings.py” contains settings to configure Django.
The “urls.py” file routes the requests based on the URL.
We generally won’t edit the first three files.
Run the Django Project
Now, we are ready to run our project for the first time. From the project directory, run
python manage.py runserver to run the development server.
Let’s open the browser to see the result. In the browser, I will type “localhost:8000”, and we can see the default screen that Django makes when we create a new Django project.
Here is the GitHub repository where you can find the complete source code of this article, clone the project and run it on your machine.