Django is a free and open-source web framework written in Python with its core principles being scalability, re-usability, and rapid development. It is also known for its framework-level consistency and loose coupling, allowing for individual components to be independent of one another.
In this tutorial, we will set up a Django environment for development purposes on an Ubuntu 20.04 server. For a live website, you will have additional considerations, including connecting to a database, setting up a domain name, and adding layers of security.
In order to complete this tutorial, you will need:
- A non-root user account with
sudoprivileges, which you can achieve by following and completing the initial server setup for Ubuntu 20.04 tutorial.
- Python 3 set up with a virtual programming environment. You can get this set up via our Python 3 installation guide.
Step 1 — Installing Django
There are several ways to install Django, the Python package manager pip within a virtual environment.
While in the server’s home directory, we’ll create the directory that will contain our Django application. Run the following command to create a directory called
django-apps, or another name of your choice. Then navigate to the directory.
mkdir django-apps cd django-apps
While inside the
django-apps directory, create your virtual environment. We’ll call it the generic
env, but you should use a name that is meaningful for you and your project.
virtualenv -p python3 env
Now, activate the virtual environment with the following command:
You’ll know it’s activated once the prefix is changed to
(env), which will look similar to the following, depending on what directory you are in:
Within the environment, install the Django package using pip. Installing Django allows us to create and run Django applications.
pip install django
Once installed, verify your Django installation by running a version check:
This, or something similar, will be the resulting output:
With Django installed on your server, we can move on to creating a test project to make sure everything is working correctly. We’ll be creating a skeleton web application.
Step 2 — Adjusting Firewall Settings
If you followed our initial server setup tutorial or have a firewall running on your server, we’ll need to open the port we’ll be using in our server’s firewall. For the UFW firewall, you can open the port with the following command:
sudo ufw allow 8000
Step 3 — Starting the Project
We now can generate an application using
django-admin, a command-line utility for administration tasks in Python. Then we can use the
startproject command to create the project directory structure for our test website.
While in the
django-apps directory, run the following command:
django-admin startproject djangosite
Now we can look to see what project files were just created. Navigate to the
testsite the directory then lists the contents of that directory to see what files were created:
cd djangosite ls Output manage.py djangosite
You will notice output that shows this directory contains a file named
manage.py and a folder named
manage.py the file is similar to
django-admin and puts the project’s package on
sys.path. This also sets the
DJANGO_SETTINGS_MODULE environment variable to point to your project’s
You can view the
manage.py the script in your terminal by running the less a command like so:
When you’re finished reading the script, press
q, to quit viewing the file.
Now navigate to the
testsite directory to view the other files that were created:
cd djangosite/ ls djangosite
Then run the following command to list the contents of the directory:
Output __init__.py asgi.py settings.py urls.py wsgi.py
Step 4 — Configuring Django
Now we can start the server and view the website on a designated host and port by running the
We’ll need to add your server IP address to the list of
ALLOWED_HOSTS in the
settings.py file located in
As stated in the Django docs, the
ALLOWED_HOSTS variable contains “a list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly safe web server configurations.”
You can use your favorite text editor to add your IP address. For example, if you’re using
nano, run the following command:
Once you run the command, you’ll want to navigate to the Allowed Hosts Section of the document and add your server’s IP address inside the square brackets within single or double-quotes.
""" Django settings for testsite project. Generated by 'django-admin startproject' using Django 2.0. ... """ ... # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True # Edit the line below with your server IP address ALLOWED_HOSTS = ['your-server-ip'] ...
Step 5 — Accessing the Django Web App
With our configuration completed, be sure to navigate back to the directory where
manage.py is located:
python manage.py runserver 0.0.0.0:8000
Finally, you can navigate to the below link to see what your skeleton website looks like, again replacing the highlighted text with your server’s actual IP:
In this tutorial you have successfully installed Django and set up a development environment to begin working on your Django app.
You now have the foundation needed to get started building Django web applications.