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

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

We will set up a Django development environment, but first, please make sure that you have a non-root user account with sudo privileges set up on a Debian or Ubuntu Linux server.

Open your terminal using:

Ctrl+Alt+T
And run the following commands:

Install Python and pip

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

This updates the local APT repository. When prompted to configure grub-pc, you can press ENTER to accept the default, or configure as desired.

sudo apt-get install python3

Verify the successful installation of Python 3:
python3 -V

sudo apt-get install -y python3-pip

Verify that pip was successfully installed:
pip3 -V

Install virtualenv

virtualenv is a virtual environment where you can install software and Python packages in a contained development space.

pip3 install virtualenv

Verify that the installation has completed successfully:

virtualenv --version

Install Django

sudo pip3 install django

Verify your Django installation by running a version check:

django-admin --version

Create a Django Test Project

Open the port we’ll be using in our server’s firewall:

sudo ufw allow 8000

Create a Directory for the Project:
mkdir test_django_app

cd test_django_app/

While in the test-django-app directory, run the following command:

django-admin startproject testsite

Navigate to the testsite directory then list the contents of that directory to see what files were created:

cd testsite

ls

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

less manage.py

Navigate to the testsite directory to view the other files that were created:

ls

You will get something like this:

Output
__init__.py  settings.py  urls.py  wsgi.py
  • 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.

Now we can start the server and view the website on a designated host and port by running the runserver command. Add your server ip address to the list of ALLOWED_HOSTS in the settings.py file located in

**~/test_django_app/testsite/testsite/.**

nano ~/test_django_app/testsite/testsite/settings.py

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 1.11.3.
...
"""
...
# 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']
...

Save the change and exit nano by holding down the CTRL + x keys and then pressing the ** y **key.

Navigate back to the directory where manage.py is located:

cd ~/test_django_app/testsite/

Replace the your-server-ip text with the IP of your server:

python3 manage.py runserver your-server-ip:8000

avigate to the below link to see what your skeleton website looks like, again replacing your-server-ip text with the IP of your server:

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

You should see:

""