Easy steps to install the Django Web Framework on CentOS 7

Easy steps to install the Django Web Framework on CentOS 7

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. It was built by experienced developers, it has capability to take care of the hassle of Web development, so you can focus on writing your app without needing to change the wheel. It’s free and open source.

Why Django

  • Ridiculously fast
  • Reassuringly secure
  • Exceedingly scalable

Installing Django

The installation method, we will discuss below rely on the EPEL repository for CentOS and RedHat-like distributions. The EPEL repository contains extra packages not maintained as part of the core distribution, which is fairly sparse.

Configuring access to the EPEL repository is significantly simpler that it has been in the past. On your server, you can configure yum to use the EPEL repository by typing:

yum install epel-release

After executing this command, You can access to all of the applications maintained within the EPEL repository.

Different Methods of installing Django

There are a number of different ways in installing Django, depending upon our needs and how we want to configure our development environment. These have different advantages and one method may lend itself better to your specific situation than others.

Global Install from Packages: The EPEL repository contain Django packages that can be installed easily with the conventional yum package manager. This is very simple, but not as flexible as some other methods also, the version contained in the repositories may be behind the official versions available from the project.

Global Install through pip: The pip tool is a package manager for Python packages. If you install pip, you can easily install Django on the system level for use by any user. This should always contain the latest stable release. Even so, global installations are inherently less flexible.

Install through pip in a Virtualenv: The Python virtual environment package allows you to create self-contained environments for various projects. Using this technology, you can install Django in a project directory without affecting the greater system. This allows you to provide per-project customization and packages easily. Virtual environments add some slight mental and process overhead in comparison to globally accessible installation, but provide the most flexibility.

Development Version Install through Git: If you wish to install the latest development version instead of the stable release, you will have to acquire the code from the git repo. This is necessary to get the latest features/fixes and can be done globally or locally. Development versions do not have the same stability guarantees.

With the above methods and qualities, select the installation method that best suites our needs out of the below instructions.

Global Install from Packages

If you wish to install Django using the EPEL repository, the process is very straight forward.

You can simply use the yum package manager to download and install the relevant packages:

yum install python-django

After completion the execution you will get the below result as

Installed:
  python2-django.noarch 0:1.11.20-1.el7

Dependency Installed:
  python-django-bash-completion.noarch 0:1.11.20-1.el7                           pytz.noarch 0:2016.10-2.el7

Complete!

To test that the installation was successful use

django-admin --version

Output:

[root@server ~]# django-admin --version
1.11.20
[root@server ~]#

This means that the software was successfully installed. You may also notice that the Django version is not the latest stable.

Global Install through pip

If you want to install the latest version of Django globally, the better option is to use pip, the Python package manager. So, we need to install the pip package manager first.

You can install pip from the EPEL repositories by using:

yum install python-pip

Once you have pip, you can easily install Django globally by using:

sudo pip install django

You can verify that the installation was successful by using:

django-admin --version

with tin the result you can see, the version available through pip is more up-to-date than the one from the EPEL repository.

Install through pip in a Virtualenv

The most flexible way to install Django on your system is with the virtualenv tool. This tool allows you to create virtual Python environments where you can install any Python packages you want without affecting the rest of the system. This allows you to select Python packages on a per-project basis regardless of conflicts with other project’s requirements.

We will begin by installing pip from the EPEL repository:

yum install python-pip

Once pip is installed, you can use it to install the virtualenv package by typing:

pip install virtualenv

Now, whenever you start a new project, you can create a virtual environment for it. Start by creating and moving into a new project directory:

mkdir ~/project_name
cd ~/project_name

Now, create a virtual environment within the project directory by typing:

virtualenv newenv

This will install a standalone version of Python, as well as pip, into an isolated directory structure within your project directory. We chose to call our virtual environment newenv, but you should name it something descriptive. A directory will be created with the name you select, which will hold the file hierarchy where your packages will be installed.

To install packages into the isolated environment, you must activate it by using:

source newenv/bin/activate

Your prompt should change to reflect that you are now in your virtual environment. It will look something like (newenv)username@hostname:~/newprojectname$.

In your new environment, you can use pip to install Django.

pip install django

You can verify the installation by typing:

django-admin --version

To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:

deactivate

Your prompt should revert to the conventional display. When you wish to work on your project again, you should re-activate your virtual environment by moving back into your project directory and activating:

cd ~/project_name
source newenv/bin/activate

Development Version Install through git

If you want a development version of Django, you will have to download and install Django from its git repository.

so, you will need to install git on your system with yum. We will also install the pip Python package manager. We will use this to handle the installation of Django after it has been downloaded:

yum install git python-pip

Once you installed git, you can clone the Django repository. Between releases, this repository will have more up-to-date features and bug fixes at the possible expense of stability. You can clone the repository to a directory called django-dev within your home directory by using:

git clone git://github.com/django/django ~/django-dev

Once the repository is cloned, you can install it using pip. We will use the -e option to install in “editable” mode, which is needed when installing from version control:

pip install -e ~/django-dev

You can verify that the installation was successful,

django-admin --version

Creating a Sample Project

We have Django installed on our server,  no we can see briefly how to get started on a project.

Use the django-admin command to create a project.

django-admin startproject your_projectname
cd your_projectname

This will create a directory called your_projectname within your current directory. Within this, a management script will be created and another directory called your_projectname will be created with the actual code.

Note: If you were already in a project directory that you created for use with the virtualenv command, you can tell Django to place the management script and inner directory into the current directory without the extra layer by typing a dot at the end

django-admin startproject projectname .

To bootstrap the database on more recent versions of Django, you can use:

python manage.py migrate

You will be asked to create an administrative user as part of this process. Select a username, email address, and password for the user.

If you used the migrate command above, you’ll need to create the administrative user manually. You can create an administrative user by using.

python manage.py createsuperuser

You will be prompted for a username, an email address, and a password for the user.

You can start up the Django development server to see what a fresh Django project looks like. You should only use this for development purposes. Run:

python manage.py runserver 0.0.0.0:8000

Visit your server’s IP address followed by :8000 in your web browser

http://ip_address:8000

If you see something like this, you have succeeded in installing django.

Easy steps to install the Django Web Framework on CentOS 7

Now, append /admin to the end of your URL to get to the admin login page:

http://ip_address:8000/admin

 

 

Easy steps to install the Django Web Framework on CentOS 7

When you are finished looking through the default site, you can stop the development server by typing CTRL-C in your terminal stop Django

The Django project you’ve created provides the structural basis for designing a more complete site. Check out the Django documentation for more information about how to build your applications and customize your site.

 

 

You may also like...