By Lisa Tagliaferri and Tony Tran
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, you will set up a Django environment for development purposes on an Ubuntu 22.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. There are a variety of tutorials on Django that can help support you as you build under the Django tag.
In order to complete this tutorial, you will need:
A non-root user account with
sudo privileges, which you can achieve by following and completing the initial server setup for Ubuntu 22.04 tutorial.
Python 3 set up with a virtual programming environment. You can get this set up via the Python 3 installation guide.
There are several ways to install Django, the Python package manager pip within a virtual environment.
While in the server’s home directory, you’ll create the directory that will contain your 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. You’ll call it the generic
env, but you should use a name that is meaningful for you and your project.
- virtualenv env
Now, activate the virtual environment with the following command:
- . env/bin/activate
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:
- django-admin --version
This, or something similar, will be the resulting output:
With Django installed on your server, you can move on to creating a test project to make sure everything is working correctly. You’ll be creating a skeleton web application.
If you followed the initial server setup tutorial or have a firewall running on your server, you’ll need to open the port you’ll be using in your server’s firewall. For the UFW firewall, you can open the port with the following command:
- sudo ufw allow 8000
If you’re using DigitalOcean Firewalls, you can select HTTP from the inbound rules. You can read more about DigitalOcean Firewalls and how to create rules for them by modifying the inbound rules.
You now can generate an application using
django-admin, a command line utility for administration tasks in Python. Then you can use the
startproject command to create the project directory structure for your test website.
While in the
django-apps directory, run the following command:
- django-admin startproject testsite
Note: Running the
django-admin startproject <projectname> command will name both project directory and project package the
<projectname> and create the project in the directory in which the command was run. If the optional
<destination> parameter is provided, Django will use the provided destination directory as the project directory, and create
manage.py and the project package within it.
Now you can look to see what project files were just created. Navigate to the
testsite directory then list the contents of that directory to see what files were created:
- cd testsite
You will notice the output that shows this directory contains a file named
manage.py and a folder named
manage.py 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 script in your terminal by running the
less command like so:
- less manage.py
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 testsite/
Then run the following command to list the contents of the directory:
You will see four files:
Output__init__.py asgi.py settings.py urls.py wsgi.py
You can go over what each of these files are:
__init__.pyacts as the entry point for your Python project.
asgi.pycontains the configuration for the optional deployment to the Asynchronous Server Gateway Interface or ASGI, which provides a standard for apps that are either synchronous and asynchronous, and is considered to be a successor of WSGI (see below).
settings.pydescribes the configuration of your Django installation and lets Django know which settings are available.
urlpatternslist, that routes and maps URLs to their
wsgi.pycontains the configuration for the Web Server Gateway Interface or WSGI, which provides a standard for synchronous Python apps.
Note: Although default files are generated, you still have the ability to tweak the
wsgi.py files at any time to fit your deployment needs.
Now you can start the server and view the website on a designated host and port by running the
You’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:
- nano ~/django-apps/testsite/testsite/settings.py
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 4.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'] ...
You can save the change and exit nano by holding down the
x keys and then pressing the
y key. Next, you’ll go on to access your web app via a browser.
Finally, create an administrative user so that you can use the Django admin interface. This can be done with the
- python manage.py createsuperuser
You will be prompted for a username, an email address, and a password for your user.
With your configuration completed, be sure to navigate back to the directory where
manage.py is located:
- cd ~/django-apps/testsite/
Now, run the following command replacing the your-server-ip text with the IP of your server:
- python manage.py runserver your-server-ip: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:
Once the page loads, you’ll see the following:
This confirms that Django was properly installed and your test project is working correctly.
To access the admin interface, add
/admin/ to the end of your URL:
This will take you to a login screen:
If you enter the admin username and password that you just created, you will have access to the main admin section of the site:
For more information about working with the Django admin interface, please see “How To Enable and Connect the Django Admin Interface.”
When you are done with testing your app, you can press
C to stop the
runserver command. This will return you to your programming environment.
When you are ready to leave your Python environment, you can run the
Deactivating your programming environment will put you back to the terminal command prompt.
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.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Django is a free and open-source web framework written in Python. Django’s core principles are 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. Don’t repeat yourself (DRY programming) is an integral part of Django principles.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Click below to sign up and get $200 of credit to try our products over 60 days!
Before running the ‘createsuperuser’ command, it is necessary to run the ‘migrate’ command.