How to Install Django Web Framework on CentOS 7

How to install Django on CentOS 7
How to install Django on CentOS 7

Install Django on CentOS 7

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 CentOS 7 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 CentOS 7. 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 yum install tree

Install Python 3

In CentOS Python 2.7 is by default installed as it is the critical part of CentOS base system. Here we need to install Python 3.6 for Django Installation. To install Python 3.6 we need to enable SCL repository (Software Collections Repository) which allows to install Python 3.6 retaining Python 2.7 so the base system will work properly.

Enable SCL repository installing CentOS 7 release SCL file using following command:

sudo yum install centos-release-scl

Now as the repository is enabled install Python 3.6 running following command:

sudo yum install rh-python36

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 you should launch new shell using scl tool to access Python 3.6

scl enable rh-python36 bash

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

tree  newdjangoapp/

Output should be

newdjangoapp/
|-- manage.py
`-- mydjangoapp
    |-- __init__.py
    |-- settings.py
    |-- urls.py
    `-- wsgi.py

Now go inside newdjangoapp directory.

cd newdjangoapp

Now we need to migrate the database.

python manage.py migrate

Output should be:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

Create administrative user running following command.

python manage.py createsuperuser

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 29, 2018 - 08:55:33
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
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
django admin login page

Stop the development server pressing Ctrl+C in terminal.

Django home for admin
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 CentOS 7. If you have any queries please don’t forget to comment below.

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