Install Django on Debian 9

Django is the most popular web framework which is designed to develop fully featured Python Web Applications. By using Django you can build secure, scalable and maintainable dynamic web applications. In this tutorial, you are going to install Django on Debian 9 using Python Virtual Environment. The best thing to use Python Virtual Environment is you can create multiple Django Environments on a single computer without affecting other Django projects. It also will become easier to install a specific module for each project.

Prerequisites

Before you start to install Django on Debian 9. You must have the non-root user account on your system with sudo privileges.

Install tree command to use it in further tutorial for better understanding.

sudo apt install tree

Confirm Python Installation and Install venv

Python 3.5 is by default installed on Debian 9. Confirm the Python installation and check the Python version by typing following command.

python3 -V

Output should be as give below. Note version number may vary.

Output:
Python 3.5.2

By using venv module we can create virtual environments in Python 3.6. To get venv module we need to install python3-venv package to do so enter following command.

sudo apt install python3-venv

Now we can create Virtual Environment for Django Applications.

Create Virtual Environment

Create a new directory for your Django application and go inside the directory.

mkdir new_django_app && cd new_django_app

Now create virtual Environment by running following command. It will create directory named venv which includes supporting files, Standard python library, Python binaries, Pip package manager.

python3 -m venv venv

To start using the virtual environment we need to activate it. To activate the virtual environment run following command.

source venv/bin/activate

Now your path will change and it will show the name of your virtual environment (venv)

Install Django

Now install Django by using Pip (Python Package Manager).

pip install Django

Confirm the installation and Check the version typing following command.

python -m django --version

The output should be as given below. NOTE: you can get slightly different output.

Output:
2.1.4

Creating Django Project

Create a Django project by using django-admin utility named newdjangoapp. Enter following command to create new Django project.

django-admin startproject newdjangoapp

Now newdjangoapp directory will be created. Check the directory structure by using the following command. This directory has manage.py file used to manage the project and other Django specific files about database configuration settings, routes, and settings

NOTE: Above command can prompt you for Username, Password and Email Address for your user.

Testing the development server

Run development server using following command.

python manage.py runserver

The output should be:

Performing system checks...
System check identified no issues (0 silenced).
December 28, 2018 - 17:00:23
Django version 2.1.4, using settings 'mydjangoapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

NOTE: If you are using the virtual machine then you need to add your server IP address inside settings.py file.

Go to http://127.0.0.1:8000/ in your browser you will get following page.

django home page

You can go to admin page by visiting http://127.0.0.1:8000/admin/ page.

Enter username and password we have created recently after successful authentication you will be redirected to the administrative page.

django admin login page

Stop the development server pressing Ctrl+C in terminal.

Django home for admin

Deactivate The Virtual Environment

To deactivate virtual environment after work run following command.

deactivate

Conclusion

You have successfully learned how to install Django Web Framework on Debian 9. If you have any queries please don’t forget to comment below.

NOTE: You can create multiple development environments repeating above steps.