Tutorial

How To Install Django and Set Up a Development Environment on Ubuntu 22.04

How To Install Django and Set Up a Development Environment on Ubuntu 22.04
Not using Ubuntu 22.04?Choose a different version or distribution.
Ubuntu 22.04

Introduction

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.

Prerequisites

In order to complete this tutorial, you will need:

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, 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.

  1. mkdir django-apps
  2. 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.

  1. virtualenv env

Now, activate the virtual environment with the following command:

  1. . 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.

  1. pip install django

Once installed, verify your Django installation by running a version check:

  1. django-admin --version

This, or something similar, will be the resulting output:

Output
4.0.6

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.

Step 2 — Adjusting Firewall Settings

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:

  1. 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.

Step 3 — Starting the Project

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:

  1. 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:

  1. cd testsite
  1. ls
Output
manage.py testsite

You will notice the output that shows this directory contains a file named manage.py and a folder named testsite. The 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 settings.py file.

You can view the manage.py script in your terminal by running the less command like so:

  1. 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:

  1. cd testsite/

Then run the following command to list the contents of the directory:

  1. ls

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__.py acts as the entry point for your Python project.
  • asgi.py contains 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.py describes the configuration of your Django installation and lets Django know which settings are available.
  • urls.py contains a urlpatterns list, that routes and maps URLs to their views.
  • wsgi.py contains 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 asgi.py or wsgi.py files at any time to fit your deployment needs.

Step 4 — Configuring Django

Now you can start the server and view the website on a designated host and port by running the runserver command.

You’ll need to add your server ip address to the list of ALLOWED_HOSTS in the settings.py file located in ~/test_django_app/testsite/testsite/.

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:

  1. 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.

settings.py
"""
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 CTRL + 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 createsuperuser command:

  1. python manage.py createsuperuser

You will be prompted for a username, an email address, and a password for your user.

Step 5 — Accessing the Django Web App

With your configuration completed, be sure to navigate back to the directory where manage.py is located:

  1. cd ~/django-apps/testsite/

Now, run the following command replacing the your-server-ip text with the IP of your server:

  1. 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:

http://your-server-ip:8000/

Once the page loads, you’ll see the following:

Django Default Page

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:

http://your_server_ip:8000/admin/

This will take you to a login screen:

Django admin login

If you enter the admin username and password that you just created, you will have access to the main admin section of the site:

Django admin page

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 CTRL + 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 deactivate command:

  1. deactivate

Deactivating your programming environment will put you back to the terminal command prompt.

Conclusion

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.

Learn more about our products


Tutorial Series: Django Development

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.

About the authors

Default avatar
Tony Tran

author


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
1 Comments


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!

Before running the ‘createsuperuser’ command, it is necessary to run the ‘migrate’ command.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.