• Get In Touch
June 21, 2016

How to Install Django Web Framework on CentOS 7

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Django is a fully featured Python based web framework which helps in rapid development and clean design.
Web frameworks like Django provide a set of tools which helps the developer to write the application faster as the framework takes care of the internal structure, thus the developer needs to take care of the application development only. Django is free and open source software.

Features of Django

Django is very popular python web framework tools because of these main features.

  • Development using Django is very fast as it is designed to help the developers to complete their application as quickly as possible.
  • Django comes with the tools which you can use to handle common web development tasks like user authentication, sitemaps, content administration, API and many more.
  • Django helps the developer to avoid common security mistakes like SQL injection, Cross site scripting, Clickjacking etc.
  • Its user authentication system provides a secure way to manage user accounts and passwords.
  • Django can be easily scaled to handle very heavy traffic.

We can install Django through various methods like installing through the packages available in default EPEL repository or installing through pip globally or installing through pip in Virtualenv. If we are installing it through packages available through the default EPEL repository then we may not get the latest version of software. If you choose to install it globally through pip then you will get the latest version of software. pip is a package manager tool for Python packages. pip will install Django globally for any user to use, global installs are less flexible compared to Virtualenv installs.

virtualenv is a Python package which is used to create and manage self contained virtual Python environments. Using this tool you can install Django in a single project directory or virtual environment without affecting the whole system. This will allow you to customise the need of different projects separately. Installation of Django using this method is recommended as it provides the most flexibility in building projects.

In this tutorial we are going to learn to install the latest version of Django web framework on CentOS 7.x server using pip globally and using pip with virtualenv.

In this guide we will be using a non root account to install Django. If you are logged in to your server using root account, omit using sudo command from all the commands.

Installing Django Globally using pip

First thing to do is to update the yum repository and add the EPEL repository in our system, as pip package is available in this repository. Execute the following command to do so –

    sudo yum -y update
    sudo yum -y install epel-release

Now you can install pipusing the following command –

    sudo yum -y install python-pip

After installation of pip you can install Django globally using the following command –

    sudo pip install django

You will get a successful message in terminal saying Successfully installed django-1.x.x. If you want to install any older version of Django instead of the latest version, you can execute the following command.

    sudo pip install django==x.x.x

Where x.x.x is the version you want to install. For example if you want to install Django version 1.8.6, you can use the following command –

    sudo pip install django==1.8.6

You can also check the version of Django by executing the following command in terminal –

    django-admin version

It will show you the installed version of Django.

Installing Django in Virtualenv Using pip

You can install Django on your system with virtualenv package. Update yum repository and add EPEL repository using the following command.

    sudo yum -y update
    sudo yum -y install epel-release

Now you can install pipusing the following command.

    sudo yum -y install python-pip

After installing pip you can install virtualenv using the following command.

    sudo pip install virtualenv

You will get successful message similar to shown below.

    Successfully installed virtualenv-15.0.2

Now start your new project by creating a directory. Once we have created the directory we can setup virtual environment inside the directory. Run the following command to create a new directory in home directory of user and move into the new directory.

    cd ~
    mkdir myproject
    cd myproject

myproject is an example name we have provided to our project directory, you can use any name instead of myproject. Now you can create your python virtual environment using the following command –

    virtualenv mydjangoproject

mydjangoproject is a sample name we have provided for our virtual environment, you can use any descriptive name instead of mydjangoproject. Once you execute the above command you will see following output.

    New python executable in /home/user/myproject/mydjangoproject/bin/python
    Installing setuptools, pip, wheel...done.

We now have local version of python along with local pip installed in our project directory. You can also view the files of our virtual environment using ls command. Now we will need to activate the virtual environment using the following command.

    source mydjangoproject/bin/activate

Now you should see that your terminal has now gone into virtual environment. You will see something similar to this.

    (mydjangoproject) [user@hostname myproject]$

Once we are in the virtual environment, we can install Django using pip by executing the following command.

    pip install django

If you want to install any older version of Django instead of the latest version, you can execute the following command –

    pip install django==x.x.x

You can verify the if Django is installed or not by checking the version of Django using the following command –

    django-admin version

You can always execute the following command to leave the virtual environment –

    deactivate

Whenever you want to go to your virtual environment, you can always change your directory to your project directory by executing cd ~/myproject and then activating your virtual environment by executing source mydjangoproject/bin/activate.

Creating a Django project

Now that we have Django installed we can create a sample project to start with. If you have installed Django globally using pip then use the following command to create a project.

    django-admin startproject myproject

The above commands will create your project and will add myproject directory. Within this directory it will create manage.py file which is a command line utility to help you manage your project. It will also create another directory with same name into the project directory. This is the directory in which the actual project files will be stored. There will be these four files created initially.

  • init.py – An empty file to tell Python that directory should be considered as Python packages.
  • settings.py – A file containing the the configuration of your project.
  • urls.py – This file will contain the URL structure of your Django project.
  • wsgi.py – This file will help WSGI-compatible web servers to serve your project.

You can view the list of the files using ls command.

If you have installed Django on Virtual environment then you will need to activate the environment and then execute the following command to start a new project.

    django-admin startproject myproject .

This will create project files in the myproject directory within the project directory and the management script will be created in the project directory. Putting dot (.) at end is very important to create the project files in the same directory.

Now we will need to transfer the database of our project to SQLite database, execute the following command –

    python manage.py migrate

You will see following output once the command is successfully execute.

    Operations to perform:
      Apply all migrations: admin, contenttypes, auth, sessions
    Running migrations:
      Rendering model states... DONE
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... 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 sessions.0001_initial... OK

Now you will need to create an administrative user for your Django web based admin panel of your project. Execute the following command to do the same.

    python manage.py createsuperuser

You will need to provide a username, email and password for your administrative user.

Now you can run your development server to run your Django project. Execute the following command to do so –

    python manage.py runserver 0.0.0.0:8080

This will start a local server on port number 8080. You will see following output on terminal –

    Performing system checks...

    System check identified no issues (0 silenced).
    June 20, 2016 - 19:51:08
    Django version 1.9.7, using settings 'myproject.settings'
    Starting development server at http://0.0.0.0:8080/
    Quit the server with CONTROL-C.

Now you can go to the following web address to access your webserver using your favorite browser.

    http://server_IP_addr:8080

You will see something similar to this.

HP_NO_IMG/data/uploads/users/2a78e75d-343a-47ce-9d84-14a6ba54abbc/2092532560.png” alt=”” />

Now to access administrative dashboard go to following address –

    http://server_IP_addr:8080/admin

You will see the login page of admin panel –

HP_NO_IMG/data/uploads/users/2a78e75d-343a-47ce-9d84-14a6ba54abbc/92087975.png” alt=”” />

Enter the username and password you have created recently, and click Log in button. You will be now logged into Django admin panel, which will look similar to this.

HP_NO_IMG/data/uploads/users/2a78e75d-343a-47ce-9d84-14a6ba54abbc/1601594918.png” alt=”” />

Conclusion

In this tutorial we have installed Django web framework on CentOS 7.x, both globally and on a virtual environment using pip. Now you can easily deploy Django on CentOS 7.x and you can also create a sample Django project.

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.
Get a Cloud Server

Share this Article!

Related Posts

Node.js Authentication – A Complete Guide with Passport and JWT

Node.js Authentication – A Complete Guide with Passport and JWT

Truth be told, it’s difficult for a web application that doesn’t have some kind of identification, even if you don’t see it as a security measure in and of itself. The Internet is a kind of lawless land, and even on free services like Google’s, authentication ensures that abuses will be avoided or at least […]

Node.js and MongoDB: How to Connect MongoDB With Node

Node.js and MongoDB: How to Connect MongoDB With Node

MongoDB is a document-oriented NoSQL database, which was born in 2007 in California as a service to be used within a larger project, but which soon became an independent and open-source product. It stores documents in JSON, a format based on JavaScript and simpler than XML, but still with good expressiveness. It is the dominant […]

Using MySQL with Node.js: A Complete Tutorial

Using MySQL with Node.js: A Complete Tutorial

Although data persistence is almost always a fundamental element of applications, Node.js has no native integration with databases. Everything is delegated to third-party libraries to be included manually, in addition to the standard APIs. Although MongoDB and other non-relational databases are the most common choice with Node because if you need to scale an application, […]

Node.Js Vs Django: Which Is the Best for Your Project

Node.Js Vs Django: Which Is the Best for Your Project

Django and NodeJs are two powerful technologies for web development, both have great functionality, versatile applications, and a great user interface. Both are open source and can be used for free. But which one fits your project best? NodeJs is based on JavaScript, while Django is written in Python. These are two equally popular technologies […]

Nodejs Vs PHP:  Which Works Best?

Nodejs Vs PHP: Which Works Best?

Before getting into the “battle” between Node.js and PHP we need to understand why the issue is still ongoing. It all started with the increased demand for smartphone applications, their success forcing developers to adapt to new back-end technologies that could handle a multitude of simultaneous requests. JavaScript has always been identified as a client-side […]