How To Install and Configure Django in Linux

To Install and Configure Django Web Framework with Virtual Environments in Ubuntu/Debian

Django is a powerful framework for writing Python web applications. Django allows your applications and sites to up and running quicker without worry about the common structural code to tie it together. Web frameworks were designed to make development easy and more effective for programmers to create their applications.This tutorial covers the ground on the installation process of Django framework.

1. To install Django

Install Django in the virtual environment using pip tool to avoid conflicts between projects. pip is a popular tool for managing python packages.

To install packages for python virtual environments, execute the following commands.
Enable the EPEL repository before installing Django On Redhat based derivatives

# yum install epel-release

On CentOS

# yum install python-pip virtualenv virtualenvwrapper

On Fedora

# dnf install python-pip virtualenv virtualenvwrapperives

On Ubuntu

Run the following commands to install it.

root@linuxhelp:~# aptitude update
Hit http://in.archive.ubuntu.com wily InRelease
Get: 1 http://in.archive.ubuntu.com wily-updates InRelease [65.9 kB]            
Get: 2 http://security.ubuntu.com wily-security InRelease [65.9 kB]
Hit http://ppa.launchpad.net wily InRelease
Hit http://packages.x2go.org jessie InRelease                                   
Hit http://in.archive.ubuntu.com wily-backports InRelease                       
.
.
.
security/multiverse Translation-en [2,806 B]
Get: 33 http://security.ubuntu.com wily-security/restricted Translation-en [2,666 B]
Get: 34 http://security.ubuntu.com wily-security/universe Translation-en [33.7 kB]
Fetched 1,652 kB in 31s (52.8 kB/s)                                             
                            
Current status: 226 (+3) upgradable.
root@linuxhelp:~# aptitude install python-pip virtualenv virtualenvwrapper
The following NEW packages will be installed:
  libjs-sphinxdoc{a} libjs-underscore{a} python-chardet-whl{a} 
  python-colorama{a} python-colorama-whl{a} python-distlib{a} 
  .
  .
  .
Setting up python-stevedore (1.5.0-1) ...
Setting up python-virtualenv (1.11.6+ds-1) ...
Setting up python-wheel (0.26.0-1) ...
Setting up python3-virtualenv (1.11.6+ds-1) ...

2. Make a directory for storing projects

Create a directory inside your home directory for storing your projects as follows.

root@linuxhelp:~/home# cd
root@linuxhelp:~# pwd
/home/user1
root@linuxhelp:~# mkdir Django 
root@linuxhelp:~# cd Django/
root@linuxhelp:~/Django# pwd
/home/user1/Django

3. To create virtual environment

Create a virtual environment for storing your projects. After creating virtual environment, files and sub-directories will be created under the environment.

root@linuxhelp:~/Django# virtualenv myprojects
Running virtualenv with interpreter /usr/bin/python2
New python executable in myprojects/bin/python2
Also creating executable in myprojects/bin/python
Installing setuptools, pip...done.
(myprojects)root@linuxhelp:~/Django# ls -l
total 8
drwxr-xr-x 6 root root 4096 Apr 19 16:12 myprojects
root@linuxhelp:~/Django# cd myprojects/(myprojects)root@linuxhelp:~/Django/myprojects# ls -l
total 16
drwxr-xr-x 2 root root 4096 Apr 19 16:53 bin
drwxr-xr-x 2 root root 4096 Apr 19 16:12 include
drwxr-xr-x 4 root root 4096 Apr 19 16:12 lib
drwxr-xr-x 2 root root 4096 Apr 19 16:12 local

4. To Activate and Deactivate virtual environment

After creating virtual environment we need to activate it. Note that after activating, your shell prompt should change. To deactivate simply type the following deactivate command.

root@linuxhelp:~/Django# source /home/user1/Django/myprojects/bin/activate
(myprojects)root@linuxhelp:~/Django# deactivate
root@linuxhelp:~/Django#

5. To install Django:

After activating virtual environment, install the django by using pip command.

(myprojects)root@linuxhelp:~/Django# pip install django
Downloading/unpacking django
  Downloading Django-1.9.5-py2.py3-none-any.whl (6.6MB): 6.6MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up...

6. To import django into python

Go to the python prompt by executing the following command and import django into your python prompt in an active virtual environment.

(myprojects)root@linuxhelp:~/Django# python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type " help" , " copyright" , " credits"  or " license"  for more information.
> > >  import django
> > >  print(django.get_version())
1.9.5
> > >  exit()
(myprojects)root@linuxhelp:~/Django#

7. To create a project in django

To start a new project using django run the below command. This will create the entire directory structure along with sub-directories and files under your virtual environment with the same name of your project.
file: _init_.py &ndash This is an empty file tells Python that this directory should be considered a Python package.
file: settings.py &ndash This is the settings file for django project.
file: urls.py &ndash This is the table of contents for your django powered site.
file: wsgi.py &ndash This is an entry point for WSGI compatible web servers to server your project.

(myprojects)root@linuxhelp:~/Django# django-admin startproject sampleproject
(myprojects)root@linuxhelp:~/Django# ls
myprojects  sampleproject
(myprojects)root@linuxhelp:~/Django# cd sampleproject/
(myprojects)root@linuxhelp:~/Django/sampleproject# ls -l
total 8
-rwxr-xr-x 1 root root  256 Apr 19 17:22 manage.py
drwxr-xr-x 2 root root 4096 Apr 19 17:22 sampleproject
(myprojects)root@linuxhelp:~/Django/sampleproject# cd sampleproject/
(myprojects)root@linuxhelp:~/Django/sampleproject/sampleproject# ls -l
total 12
-rw-r--r-- 1 root root    0 Apr 19 17:22 __init__.py
-rw-r--r-- 1 root root 3187 Apr 19 17:22 settings.py
-rw-r--r-- 1 root root  769 Apr 19 17:22 urls.py
-rw-r--r-- 1 root root  403 Apr 19 17:22 wsgi.py

8. To run Django built-in Web Server

Django has a lightweight built-in web server, which is written in python language. You can use this server to test your applications during the development process. Before running the server, change directory to project directory.

(myprojects)root@linuxhelp:~# pwd
/home/user1
(myprojects)root@linuxhelp:~# cd Django/sampleproject/
(myprojects)root@linuxhelp:~/Django/sampleproject# pwd
/home/user1/Django/sampleproject
(myprojects)root@linuxhelp:~/Django/sampleproject# ls
manage.py  sampleproject
(myprojects)root@linuxhelp:~/Django/sampleproject# python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations  your app may not work properly until they are applied.
Run ' python manage.py migrate'  to apply them.

April 19, 2016 - 12:20:42
Django version 1.9.5, using settings ' sampleproject.settings' 
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

9. To Create a database file:

Run the following command to create a new file called db.sqlite3 under your current directory for django.

(myprojects)root@linuxhelp:~/Django/sampleproject# python manage.py migrate
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

(myprojects)root@linuxhelp:~/Django/sampleproject# ls
db.sqlite3   manage.py    sampleproject

Now start the server again. Here ‘ 8000‘ is the default port number. And ‘ 0.0.0.0’ is the network interface which allows the other computers in the same network to access the project, else we can use ‘ 127.0.0.1&prime to access from localhost.

(myprojects)root@linuxhelp:~/Django/sampleproject# python manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
April 19, 2016 - 12:24:26
Django version 1.9.5, using settings ' sampleproject.settings' 
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

10. To Verify

After running the server verify it in your browser by entering your local ip address with port number in the browser.

FAQ
Q
anyother django tutorials? Provide link
A
https://www.linuxhelp.com/create-web-application-using-django/ https://www.linuxhelp.com/how-to-create-basic-web-applications-using-django-part-2/
Q
can I get link for official site
A
go to this link, https://www.djangoproject.com/
Q
what is the newer version
A
Django 2.0.7 is latest
Q
need for documentation
A
https://docs.djangoproject.com/en/2.0/
Q
can I have some same softwares like this
A
yes, you can use laravel, code igniter, ruby on rails, zend framework etc