In this article, we will discuss what Django is, its purpose, and how to install and configure it on CentOS 8.
What is Django?
Django is a high-level Python framework for developing web applications in Python. It provides the necessary structure for developing future applications. Thanks to this platform, we simplify our work and many other necessary functions (like authorization). Various other purposes are already written in the framework, which lets us focus on functionality. Django is one of the most popular Python frameworks. It is ideal for small problems as well as larger enterprise solutions.
Django uses the Model-View-Controller (or MVC) design pattern. It also is quite often used for social networking software like chat rooms and other web-based applications. Applications like Instagram, Spotify, Pinterest, YouTube, Google, and others are all written using Django.
Installation on CentOS 8
Django installation calls for the following requirements:
- Server 4 GB RAM and 2 kernels.
- OS CentOS 8
- Python 3
- Pip 3
- We execute all commands as the root user. (if you run commands from a regular user, you must use the sudo command.)
To begin, update the system and application packages by running the following commands.
[root@host]# dnf update && dnf upgrade Last metadata expiration check: 1:47:26 ago on Sat 20 Feb 2021 02:22:42 AM EST. Dependencies resolved. Nothing to do. Complete! Last metadata expiration check: 1:47:27 ago on Sat 20 Feb 2021 02:22:42 AM EST. Dependencies resolved. Nothing to do. Complete! [root@host]#
Python is required to install and run Django. Install it using the following command.
[root@host]# dnf install python3 python3-pip Last metadata expiration check: 1:48:19 ago on Sat 20 Feb 2021 02:22:42 AM EST. Package python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64 is already installed. Package python3-pip-9.0.3-18.el8.noarch is already installed. Dependencies resolved. Nothing to do. Complete! [root@host]#
As we can see above, I already have Pip installed on my system and the installation notified me of it.
python36-3.6.8-2.module_el8.3.0+562+e162826a.x86_64 is already installed. Package python3-pip-9.0.3-18.el8.noarch is already installed.
But just to be sure, we will check using the python3 -V command.
[root@localhost ~]# python3 -V Python 3.6.8 [root@localhost ~]#
Verify Pip Version
Starting with Python 3.4, Pip is installed by default. Next, we will verify the version of Pip installed.
[root@localhost ~]# pip3 -V pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6) [root@localhost ~]#
We will install Django using Pip3. This python package manager helps to install and configure all of the necessary applications for working with Django.
[root@host]# pip3 install Django WARNING: Running pip install with root privileges is generally not a good idea. Try `pip3 install --user` instead. Collecting Django Downloading https://files.pythonhosted.org/packages/b8/6f/9a4415cc4fe9228e26ea53cf2005961799b2abb8da0411e519fdb74754fa/Django-3.1.7-py3-none-any.whl (7.8MB) 100% |████████████████████████████████| 7.8MB 209kB/s Collecting asgiref<4,>=3.2.10 (from Django) Downloading https://files.pythonhosted.org/packages/89/49/5531992efc62f9c6d08a7199dc31176c8c60f7b2548c6ef245f96f29d0d9/asgiref-3.3.1-py3-none-any.whl Collecting sqlparse>=0.2.2 (from Django) Downloading https://files.pythonhosted.org/packages/14/05/6e8eb62ca685b10e34051a80d7ea94b7137369d8c0be5c3b9d9b6e3f5dae/sqlparse-0.4.1-py3-none-any.whl (42kB) 100% |████████████████████████████████| 51kB 4.1MB/s Requirement already satisfied: pytz in /usr/lib/python3.6/site-packages (from Django) Installing collected packages: asgiref, sqlparse, Django Successfully installed Django-3.1.7 asgiref-3.3.1 sqlparse-0.4.1 [root@host]#
Next, we can verify the version of Django to ensure everything is installed correctly. We use the version flag with the main django-admin command.
[root@host]# django-admin --version 3.1.7 [root@host]#
Create Django Application
Now that we have installed Django, let's put it to the test. Let's create our first application using Django.
Create a project using the django-admin starttproject command. After the main command, we specify the name of the app we are building - liquidweb_app. This creates a folder called liquidweb_app/.
[root@host]# django-admin startproject liquidweb_app
Now, change directories (cd) into the project folder.
[root@host]# cd liquidweb_app/ [root@host liquidweb_app]#
Next, we perform a migrate using Python. This step is necessary in order for Django to transfer the necessary libraries and files to the project.
[root@host liquidweb_app]# python3 manage.py migrate 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 auth.0012_alter_user_first_name_max_length... OK Applying sessions.0001_initial... OK [root@host liquidweb_app]#
Next, let's create a Django application admin using the command.
[root@host liquidweb_app]# python3 manage.py createsuperuser Username: margaret Email address: email@example.com Password: Password (again): Superuser created successfully. [root@host liquidweb_app]#
Our test application is almost complete. To launch the interface in the browser, we need to configure the resolution by IP address. If you do not know your IP, find it using the ifconfig command.
[root@host liquidweb_app]# ifconfig enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.50.154 netmask 255.255.255.0 broadcast 192.168.50.255 inet6 fe80::efca:4bfb:98f8:5655 prefixlen 64 scopeid 0x20<link> ether 08:00:27:af:58:e0 txqueuelen 1000 (Ethernet) RX packets 22918 bytes 17925384 (17.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 5861 bytes 426326 (416.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@host liquidweb_app]#
Now, enter the IP into the Django config file using the editor of your choice. I am using the nano editor for this task.
[root@host liquidweb_app]# nano liquidweb_app/settings.py
Then, save and exit with the commands ctrl + s and ctrl + x (for nano).
Next, let's open ports in the firewall to access Django over the network. We will be opening ports 80, and 8000.
[root@host liquidweb_app]# firewall-cmd --permanent --add-port=80/tcp success [root@host liquidweb_app]#
[root@host liquidweb_app]# firewall-cmd --permanent --add-port=8000/tcp success [root@host liquidweb_app]#
We must reload the firewall in order for the changes to take effect.
[root@host liquidweb_app]# firewall-cmd --reload success [root@host liquidweb_app]#
Launch Django Application
Finally, we launch our Django application.
[root@host liquidweb_app]# python3 manage.py runserver 0.0.0.0:8000 Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). February 20, 2021 - 09:57:32 Django version 3.1.7, using settings 'liquidweb_app.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C.
Now, let's open our browser to the IP that we entered earlier - http: // My_IP_Address: 8000.
Previously, we created an administrator, so now we will go to the administration section, where we can test some of the test application features. Go to the following address: http://192.168.50.154:8000/admin.
Next, enter our username and password
We entered into the application as an administrator, so now we can take a closer look at all the possibilities that Django provides in our test run.
We have studied and figured out what kind of Django framework is, what it is for, and where it is used. We also installed it on CentOS 8 and ran it in test mode. Django is great at building a fast website, and it does a great job of it. It not only allows you to write quickly but also provides quality assurance. The rest is up to the developers.
Have questions? If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server, Managed Cloud Servers, or a Dedicated server owner, and you are uncomfortable with performing any of the steps outlined, we can be reached via phone at 800.580.4985, a chat or support ticket to assisting you with this process.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.