How to Install Django on CentOS 8 Web Framework (Tutorial)

In this article about how to Install Django on CentOS 8, we will also introduce Django and its features. 

What is Django Server?

Django is a Python based open source framework used by most developers to build or write custom web applications. Its simple syntax rule helps keep the code base clear and readable so making it easier for developers to design and maintain web applications. Django was released in 2003 by Adrian Holovaty and Simon Willison, comprising in built features that aid in a faster web development process.

The web framework further allows developers to build complex database driven websites with less coding. It has a ready to use user interface and supports various formats such as JSON, HTML, XML, etc. Another feature of Django is the batteries included web framework. This feature enables developers to build websites as per different business requirements.

Django server supports many relational database systems and helps build multilingual websites with its in built internationalization system. It supports various other features like predefined libraries, an integrated unit testing system, dynamic HTML pages for web framework.

Benefits of Django

The open source web framework is easy to learn and allows programmers to design custom web applications without any hassle. Below are a few additional benefits of using the Django web framework.

Written in Python: Python is one of the popular programming languages, easy to learn and implement. As the Django web framework is Python based, it gets easier for developers to build web applications and maintain code. Also, the programmers can save time by reusing the code and eliminating the need for writing additional code when building custom web applications.

Advanced security features: It holds variety of built security features and authentication systems that protect web applications from attacks and threats. The Get Method is uses it transmits the information but keeps all the passwords and security information encrypted. SQL injection, cross site scripting, and cross site request forgery are the built in features that aid the information to always stay protected.

Compatible with most operating systems and databases: People access web applications on different devices and platforms. Django ensures that the built web applications support various operating systems like Windows, Linux, macOS, etc., and are easily accessible to users. Also, the ORM system by Django ensures that the developers can work with most databases, including MySQL, Oracle, etc. You can use the system to transfer information and perform database operations without extra code required.

Large and active community support: Being a Python based framework, Django aids in reducing development costs and it is supported by a large and active developers community where there are frequent share updates related to new plugins or code snippets to simplify the development process. Using these resources and information, the programmers can speed up their process and resolve issues.

Batteries Included Framework: Django follows the batteries included approach when designing a custom web application. It uses the out of the box resources and reuses the code to save time. To perform operations, such as database manipulation, URL routing, session management and HTML templating, the framework provides codes and helps save the web application development time by reusing them. 

Easy to Extend and Scale: The framework allows developers to build large and complex web applications by making a few changes to the separated components of the web applications. It aids in customizing, scaling and extending the web framework easily. In addition the programmers can unplug or replace these decoupled components as per their project requirements.

Fast Processing: Django’s MVC (Model View Controller) core architecture helps in faster processing as it allows to put resources on a CDN while maintaining the speed.

Django provides a dynamic CRUD (create, read, update and delete) interface, configured with admin models that aids in speedy process.

Other features are:

  • An emailing system to send notifications to users.
  • High load booking engines perfect for shopping platforms.

In the next section we will show you how to install Django on CentOS 8.

How to Install Django on CentOS 8

Install Python and PIP

Django is written in Python. So Python must be installed on your server. If not installed, you can install it with the following command:

				
					dnf install python36 python3-pip -y
				
			

Once the installation is completed, verify the Python version with the following command:

				
					python3 -V
				
			

You will get the following output:

				
					Python 3.6.8
				
			

You can also verify the PIP version with the following command:

				
					pip3 -V
				
			

You should see the PIP version in the following output:

				
					pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
				
			

Install Django Using PIP

The simple and easiest way to install Django via PyPI repository. This method allows you to install the specific version of Django for your project.

To install the Django, run the following command:

				
					pip3 install Django
				
			

You will get the following output:

				
					Collecting asgiref<4,>=3.3.2 (from Django)
  Downloading https://files.pythonhosted.org/packages/fe/66/577f32b54c50dcd8dec38447258e82ed327ecb86820d67ae7b3dea784f13/asgiref-3.4.1-py3-none-any.whl
Collecting typing-extensions; python_version < "3.8" (from asgiref<4,>=3.3.2->Django)
  Downloading https://files.pythonhosted.org/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl
Installing collected packages: sqlparse, pytz, typing-extensions, asgiref, Django
Successfully installed Django-3.2.12 asgiref-3.4.1 pytz-2021.3 sqlparse-0.4.2 typing-extensions-4.1.1

				
			

After the successful installation, verify the Django version with the following command:

				
					django-admin --version
				
			

You will get the following output:

				
					3.2.12
				
			

Install Django Using Virtualenv

You can also install Django in Python virtual environment. Python Virtualenv allows you to create isolated python environments for your project. It is very useful because it allows developers to run and develop an application with different python versions.

First, install the Python virtualenv package using the following command:

				
					pip3 install virtualenv
				
			

After the installation, create a new Python virtual environment named newenv using the following command:

				
					virtualenv newenv
				
			

You will get the following output:

				
					created virtual environment CPython3.6.8.final.0-64 in 1136ms
  creator CPython3Posix(dest=/root/newenv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

				
			

Next, change the directory to newenv and activate the virtual environment with the following command:

				
					cd newenv/
source bin/activate
				
			

Next, install the Django version 3.0.0 using the following command:

				
					pip3 install django==3.0.0
				
			

You will get the following output:

				
					  Using cached pytz-2021.3-py2.py3-none-any.whl (503 kB)
Collecting typing-extensions
  Using cached typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Installing collected packages: typing-extensions, sqlparse, pytz, asgiref, django
Successfully installed asgiref-3.4.1 django-3.0 pytz-2021.3 sqlparse-0.4.2 typing-extensions-4.1.1
				
			

Next, deactivate from the virtual environment using the following command:

				
					deactivate
				
			

Create a Django Project

This section will show you how to create and run a Django project. First, create a new virtual environment named django with the following command:

				
					virtualenv django
				
			

You will get the following output:

				
					created virtual environment CPython3.6.8.final.0-64 in 202ms
  creator CPython3Posix(dest=/root/django, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
				
			

Next, change the directory to the django and activate it with the following command:

				
					cd django
source bin/activate
				
			

Next, install the Django version 3.0.0 using the following command:

				
					pip3 install django==3.0.0
				
			

You should see the following output:

				
					  Using cached Django-3.0-py3-none-any.whl (7.4 MB)
Collecting sqlparse>=0.2.2
  Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting asgiref~=3.2
  Using cached asgiref-3.4.1-py3-none-any.whl (25 kB)
Collecting pytz
  Using cached pytz-2021.3-py2.py3-none-any.whl (503 kB)
Collecting typing-extensions
  Using cached typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Installing collected packages: typing-extensions, sqlparse, pytz, asgiref, django
Successfully installed asgiref-3.4.1 django-3.0 pytz-2021.3 sqlparse-0.4.2 typing-extensions-4.1.1

				
			

Next, create a new Django project named examplesite with the following command:

				
					django-admin startproject examplesite
				
			

Next, change the directory to examplesite and edit the settings.py file:

				
					cd examplesite
nano examplesite/settings.py
				
			

Add your server IP address in the ALLOWED_HOSTS:

				
					ALLOWED_HOSTS = ["your-server-ip"]
				
			

Save and close the file, then migrate the Django database with the following command:

				
					python3 manage.py migrate
				
			

You will get the following output:

				
					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 auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

				
			

Next, create a superuser to log into the Django admin interface:

				
					python3 manage.py createsuperuser
				
			

Provide your admin username, password, and email as shown below:

				
					Username (leave blank to use 'root'): djangoadmin
Email address: admin@example.com
Password: 
Password (again): 
Superuser created successfully.

				
			

Next, start the Django project with the following command:

				
					python3 manage.py runserver 0.0.0.0:8080
				
			

You will get the following output:

				
					Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
February 24, 2022 - 13:57:11
Django version 3.0, using settings 'examplesite.settings'
Starting development server at http://0.0.0.0:8080/
Quit the server with CONTROL-C.

				
			

Access Django Web Interface

Now, open your web browser and access the Django home page using the URL http://your-server-ip. You should see the Django home page in the following screen:

To access the Django admin interface, type the URL http://your-server-ip/admin. You will be redirected to the Django login page:

Provide your admin username, password, and click on the Log in button. You should see the Django dashboard on the following screen:

How to Instal Django on CentOS 8 Conclusion

This post explained how to install Django on CentoS 8.We have explained what Django framework is, what it is for, and where it is used. Django is a great addition to projects that need to handle large volumes of content as well as user interactions. it is also beneficial for projects with handling heavy traffic or to deal with complex functions or technology (e.g., machine learning).

 

Django is simple enough to be also used for smaller scale projects or for project you intending to scale up. Django is great at building a fast website. You can now choose your preferred method to install Django on CentOS 8. 

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x