Tutorial

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

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

Introduction

Django is a free and open-source web framework written in Python that adheres to the model template view (MTV) software architectural pattern. The MTV pattern is Django’s take on the model–view–controller (MVC) pattern. According to the Django Software Foundation, the model is the single definitive source of your data, the view describes the data that gets represented to the user via a Python callback function to a specific URL, and the template is how Django generates HTML dynamically.

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.

In this tutorial, we will set up a Django development environment. We’ll install Python 3, pip 3, Django and virtualenv in order to provide you with the tools necessary for developing web applications with Django.

Prerequisites

A non-root user account with sudo privileges set up on a Debian or Ubuntu Linux server. You can achieve these prerequisites by following and completing the initial server setup for Debian 8, or steps 1-4 in the initial server setup for Ubuntu 16.04 tutorial.

Step 1 — Install Python and pip

To install Python we must first update the local APT repository. In your terminal window, we’ll input the command that follows. Note that the -y flag answers “yes” to prompts during the upgrade process. Remove the flag if you’d like the upgrade to stop for each prompt.

  1. sudo apt-get update && sudo apt-get -y upgrade

When prompted to configure grub-pc, you can press ENTER to accept the default, or configure as desired.

It is recommended by the Django Software Foundation to use Python 3, so once everything is updated, we can install Python 3 by using the following command:

  1. sudo apt-get install python3

To verify the successful installation of Python 3, run a version check with the python3 command:

  1. python3 -V

The resulting output will look similar to this:

Output
python 3.5.2

Now that we have Python 3 installed, we will also need pip in order to install packages from PyPi, Python’s package repository.

  1. sudo apt-get install -y python3-pip

To verify that pip was successfully installed, run the following command:

  1. pip3 -V

You should see output similar to this:

Output
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

Now that we have pip installed, we have the ability to quickly install other necessary packages for a Python environment.

Step 2 — Install virtualenv

virtualenv is a virtual environment where you can install software and Python packages in a contained development space, which isolates the installed software and packages from the rest of your machine’s global environment. This convenient isolation prevents conflicting packages or software from interacting with each other.

To install virtualenv, we will use the pip3 command, as shown below:

  1. pip3 install virtualenv

Once it is installed, run a version check to verify that the installation has completed successfully:

  1. virtualenv --version

We should see the following output, or something similar:

Output
virtualenv 20.0.20 from /home/sammy/.local/lib/python3.5/site-packages/virtualenv/__init__.py

You have successfully installed virtualenv.

At this point, we can isolate our Django web application and its associated software dependencies from other Python packages or projects on our system.

Step 3 — Install Django

There are three ways to install Django. We will be using the pip method of installation for this tutorial, but let’s address all of the available options for your reference.

  • Option 1: Install Django within a virtualenv. This is ideal for when you need your version of Django to be isolated from the global environment of your server.

  • Option 2: Install Django from Source. If you want the latest software or want something newer than what your Ubuntu APT repository offers, you can install directly from source. Note that opting for this installation method requires constant attention and maintenance if you want your version of the software to be up to date.

  • Option 3: Install Django Globally with pip. The option we are going with is pip 3 as we will be installing Django globally.

We’ll be installing Django using pip within a virtual environment. For further guidance and information on the setup and utilization of programming environments, check out this tutorial on setting up a virtual environment.

While in the server’s home directory, we have to 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.

  1. mkdir django-apps
  2. cd django-apps

While inside the django-apps directory, create your virtual environment. Let’s call it env.

  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. To learn more about Django, read our tutorial series on Django Development.

  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
2.2.12

With Django installed on your server, we can move on to creating a test project to make sure everything is working correctly.

Step 4 — Creating a Django Test Project

To test the Django installation, we will be creating a skeleton web application.

Setting Firewall Rules

First, if applicable, we’ll need to open the port we’ll be using in our server’s firewall. If you are using UFW (as detailed in the initial server setup guide), 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 creating rules for them by reading the inbound rules section of the introductory tutorial.

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:

  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 we 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 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 settings.py urls.py wsgi.py

Let’s go over what each of these files are:

  • __init__.py acts as the entry point for your Python project.
  • 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. The Web Server Gateway Interface (WSGI) is the Python platform standard for the deployment of web servers and applications.

Note: Although a default file was generated, you still have the ability to tweak the wsgi.py at any time to fit your deployment needs.

Start and View your Website

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

We’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, just simply 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 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']
...

You can save the change and exit nano by holding down the CTRL + x keys and then pressing the y key.

With this 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 receive a webpage that is similar to the following:

Django Default Page

This confirms that Django was properly installed and our test project is working correctly.

When you are done with testing your app, you can press CTRL + C to stop the runserver command. This will return you to the 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 upgraded to the latest version of Python 3 available to you via the Ubuntu APT repository. You’ve also installed pip 3, virtualenv, and django.

You now have the tools 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

About the authors


Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 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!

how to get to production follow by this set up ?

Thank you very much, Jeremy, Lisa… nice work!

Nicely explained for beginners. Thanks for sharing.

thank you for this. great help .

If using digital ocean firewalls - ensue to add ‘All TCP’ inbound rules (all ports) when running the server for the testtile Django file

i’m using an amazon Google Cloud Platform server and when i try to run the server with my IP (i verified my ip also with curl ipecho.net/plain ; echo), i get this error:

(venv) jfdavenport416@instance-1:~/new_django_app/testsite$ curl ipecho.net/plain ; echo
34.82.96.4
(venv) jfdavenport416@instance-1:~/new_django_app/testsite$ python3 manage.py runserver 34.8
2.96.4:8000
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
November 25, 2019 - 22:40:56
Django version 2.2.7, using settings 'testsite.settings'
Starting development server at http://34.82.96.4:8000/
Quit the server with CONTROL-C.
Error: That IP address can't be assigned to.
(venv) jfdavenport416@instance-1:~/new_django_app/testsite$ 

Great!! Thanks for your guidelines. It worked in one go!!!

File “/usr/local/lib/python2.7/dist-packages/django/conf/urls/init.py”, line 50, in include urlconf_module = import_module(urlconf_module) File “/usr/lib/python2.7/importlib/init.py”, line 37, in import_module import(name) File “/home/dania/Trial/polls/urls.py”, line 1, in <module> from django.conf.urls import patterns,url ImportError: cannot import name patterns

<h1> I got errors like these can anyone help ?? </h1>

Thanks for your guide. I have a problem: i’m using an amazon EC2 server and when i try to run the server with my IP (i verified my ip also with curl ipecho.net/plain ; echo), i get this error:

System check identified 1 issue (0 silenced).
May 16, 2018 - 13:56:01
Django version 2.0.5, using settings 'Server.settings'
Starting development server at http://my-ip:8000/
Quit the server with CONTROL-C.
**Error: That IP address can't be assigned to.**

Thank you!

I’m unable to start the server as shown above…

please help me

what is http://your-server-ip:8000/??

what is your server??? in the above line. my sysytem’s ip or 127.0.0.1? which of the following? how to know the my server ip? how to set up the server? please help me out!!!

Thanks and Regards, Krishna

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!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

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

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more