How to install and start a project with Django framework on CentOS 7

Introduction

Django is a cutting-edge Python Web framework that it high-levelness encourages rapid development and clean, pragmatic design. Django is a full-featured framework which allows you to develop sites and web applications very quickly without having to worry about the common structural code.
There are many ways for installing the Django framework such as: Installing through a package manager, Install using “git” repository, Install using the virtualenv, and Installing with pip.
In this tutorial, we are going to install Django using “pip” because apart from the ability to install the latest stable version which is very good it also provides a lot of flexibility.

We are assuming that you have root permission, otherwise, you may start commands with “sudo”.

Install EPEL repository

As we are going to install some Python packages, we need to have EPEL installed because some of them are not provided by the official repository:

yum install epel-release

Global installation through pip

If you want to install the latest version of Django you can install it with the following command, First, we need to install the pip (Python’s package manager).

yum install python-pip

Once you have pip installed you can install Django easily with the command below:

pip install django

you can check the version of you installed Django with the command below, it also verifies that the installation process was successful.

django-admin --version

Create a simple project in Django

Now that you have Django installed, we can go ahead and find out how to start a project in Django framework.
You can use the “django-admin” command to create a project (make sure to replace the red parts with your own preferred values):

django-admin startproject letsserver

You created a project named “hugeserver“, switch to it with the following command:

cd letsserver

Execute the following command in order to get the initial database instructions:

python manage.py migrate

Now you have to create an administrative user for your project with the command below:

python manage.py createsuperuser
  • Username (leave blank to use ‘root’): type your username then hit enter.
  • Email address: set the primary Email address.
  • Password: Enter a password at least 8 characters.
  • Password (again): confirm your password

If you have done everything right you get the following output:
Superuser created successfully.
Before we can start our Django project we have to define Allowed user in the Django setting.
Switch to the root directory of your project with the command below (the directory has the name same as your project):

cd letsserver

Open the setting file with your text editor:

nano setting.py

Find the line that refers to “ALLOWED_HOSTS = []”
and put your public IP address within the brackets just like below:

ALLOWED_HOSTS = ['*']

This way you can run your Django on any port you want.
Now head back to the parent directory with the command below:

cd ../

And run your Django framework with the command below:

python manage.py runserver 0.0.0.0:8000

This will start your Django on port 8000 and make it accessible through your public IP address or your Domain. you can visit it at the following URL:

http://YOUR_IP_ADDRESS:8000

You will see a page like below:


NOTE: Running Django with this method is good enough for developing, but if you want to start serving your project you may have to use a real web server that we will talk about that in the future articles.
 
Check out Django official website for more information and news!

Be the first to comment

Leave a Reply

Your email address will not be published.


*