How to Install django on CentOS 7

Django is popular Web framework for writing web applications. With django you can built faster and scalable applications, without writing the code from scratch (without reinventing the wheel). Web framework were design to aid programmer to create applications. These web framework are the core and takes care of functionalities like user session managemet, database connectivity etc.

refer the article for installing Python3: Installing Python3 and pip3 on centos 7

You can use pip to install the virtual environment and verify it using pip list.

[root@cms ~]# pip install virtualenv
Collecting virtualenv
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 236kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
[root@cms ~]#
[root@cms ~]# pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
setuptools (19.2)
virtualenv (15.1.0)

Create virtual environment using virtualenv command. This command will create a directory and setup all the files.

[root@cms ~]# virtualenv env_mysite
Using base prefix '/usr'
New python executable in /root/env_mysite/bin/python3.4
Also creating executable in /root/env_mysite/bin/python
Installing setuptools, pip, wheel...done.

Now, you can activate the virtual environment. The ssh prompt will change as below. After that you can verify the Python version.

[root@cms ~]# source env_mysite/bin/activate
(env_mysite) [root@cms ~]#
(env_mysite) [root@cms ~]# python
Python 3.4.5 (default, Oct 20 2016, 22:54:51)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
(env_mysite) [root@cms ~]#

Let’s install django into the virtual environemt.

(env_mysite) [root@cms ~]# pip install django
Collecting django
  Downloading Django-1.10.3-py2.py3-none-any.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 179kB/s
Installing collected packages: django
Successfully installed django-1.10.3

(env_mysite) [root@cms ~]# pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
Django (1.10.3)
pip (9.0.1)
setuptools (29.0.1)
wheel (0.29.0)
(env_mysite) [root@cms ~]#

Django framework has been installed, now you start a new project. This command will create a new directory in the working directory.

(env_mysite) [root@cms env_mysite]# django-admin startproject mysite
(env_mysite) [root@cms env_mysite]# tree mysite/
mysite/
├── manage.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 5 files
(env_mysite) [root@cms env_mysite]#

Start the application using the below command.

(env_mysite) [root@cms mysite]# pwd
/root/env_mysite/mysite
(env_mysite) [root@cms mysite]# python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

November 30, 2016 - 09:01:01
Django version 1.10.3, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

It’s giving notification, you also need the run the below command as well.
python manage.py migrate
python manage.py createsuperuser

By default, the application will run on the localhost. If you need to access on remote server, then the IP address needs to be added to ‘ALLOWED_HOSTS’ section in file ~/env_mysite/lib/python3.4/site-packages/django/http/request.py.

(env_mysite) [root@cms mysite]# python manage.py runserver 192.168.56.110:8000
Performing system checks...

System check identified no issues (0 silenced).

You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

November 30, 2016 - 09:09:17
Django version 1.10.3, using settings 'mysite.settings'
Starting development server at http://192.168.56.110:8000/
Quit the server with CONTROL-C.
[30/Nov/2016 09:09:33] "GET / HTTP/1.1" 200 1767
[30/Nov/2016 09:09:38] "GET / HTTP/1.1" 200 1767

Now, use a web browser to check your application:

http://192.168.56.110:8000/
http://192.168.56.110:8000//admin

Upon Success, you will get the bellow message.

It worked!
Congratulations on your first Django-powered page.

Press ‘Clt + C’ to quit the application and ‘deactivate’ command to leave the virtual environment.

(env_mysite) [root@cms mysite]# deactivate

I really like django. Hope, you will like too. It’s versatile.

One Comment

  1. krishna@ubuntu:~$ python3 –version
    Python 3.5.2
    krishna@ubuntu:~$ sudo apt-get install python3-pip
    krishna@ubuntu:~$ pip3 –version
    pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
    krishna@ubuntu:~$
    krishna@ubuntu:~$ mkdir DjangoProjects
    krishna@ubuntu:~$ cd DjangoProjects/
    krishna@ubuntu:~/DjangoProjects$ mkdir FirstProject
    krishna@ubuntu:~/DjangoProjects$ cd FirstProject/

    krishna@ubuntu:~/DjangoProjects/FirstProject$ pip3 install virtualenv
    Collecting virtualenv
    Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 575kB/s
    Installing collected packages: virtualenv
    Successfully installed virtualenv
    You are using pip version 8.1.1, however version 9.0.1 is available.
    You should consider upgrading via the ‘pip install –upgrade pip’ command.
    krishna@ubuntu:~/DjangoProjects/FirstProject$

    krishna@ubuntu:~/DjangoProjects/FirstProject$ virtualenv -p python3 env
    Already using interpreter /usr/bin/python3
    Using base prefix ‘/usr’
    New python executable in /home/krishna/DjangoProjects/FirstProject/env/bin/python3
    Also creating executable in /home/krishna/DjangoProjects/FirstProject/env/bin/python
    Installing setuptools, pip, wheel…done.
    krishna@ubuntu:~/DjangoProjects/FirstProject$

    krishna@ubuntu:~/DjangoProjects/FirstProject$ source env/bin/activate
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject$ ls -l env/bin/
    activate activate_this.py pip python python-config
    activate.csh easy_install pip3 python3 wheel
    activate.fish easy_install-3.5 pip3.5 python3.5

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject$ pip install django
    Collecting django
    Downloading Django-2.0-py3-none-any.whl (7.1MB)
    100% |████████████████████████████████| 7.1MB 172kB/s
    Collecting pytz (from django)
    Downloading pytz-2017.3-py2.py3-none-any.whl (511kB)
    100% |████████████████████████████████| 512kB 2.4MB/s
    Installing collected packages: pytz, django
    Successfully installed django-2.0 pytz-2017.3

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject$ pip list
    Django (2.0)
    pip (9.0.1)
    pytz (2017.3)
    setuptools (38.2.4)
    wheel (0.30.0)

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject$ django-admin startproject mysite
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject$ ls -ltrh
    total 8.0K
    drwxrwxr-x 5 krishna krishna 4.0K Dec 11 10:56 env
    drwxrwxr-x 3 krishna krishna 4.0K Dec 11 11:15 mysite

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject$ cd mysite/

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ python manage.py startapp polls

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ ls -ltrh
    total 12K
    -rwxrwxr-x 1 krishna krishna 538 Dec 11 11:15 manage.py
    drwxrwxr-x 3 krishna krishna 4.0K Dec 11 11:20 mysite
    drwxrwxr-x 3 krishna krishna 4.0K Dec 11 11:20 polls
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ ls -ltrh polls/
    total 24K
    -rw-rw-r– 1 krishna krishna 63 Dec 11 11:20 views.py
    -rw-rw-r– 1 krishna krishna 60 Dec 11 11:20 tests.py
    -rw-rw-r– 1 krishna krishna 57 Dec 11 11:20 models.py
    drwxrwxr-x 2 krishna krishna 4.0K Dec 11 11:20 migrations
    -rw-rw-r– 1 krishna krishna 0 Dec 11 11:20 __init__.py
    -rw-rw-r– 1 krishna krishna 85 Dec 11 11:20 apps.py
    -rw-rw-r– 1 krishna krishna 63 Dec 11 11:20 admin.py
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ ls -ltrh mysite/
    total 16K
    -rw-rw-r– 1 krishna krishna 389 Dec 11 11:15 wsgi.py
    -rw-rw-r– 1 krishna krishna 748 Dec 11 11:15 urls.py
    -rw-rw-r– 1 krishna krishna 3.1K Dec 11 11:15 settings.py
    -rw-rw-r– 1 krishna krishna 0 Dec 11 11:15 __init__.py
    drwxrwxr-x 2 krishna krishna 4.0K Dec 11 11:20 __pycache__

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ python manage.py runserver 192.168.56.102:8000
    Performing system checks…

    System check identified no issues (0 silenced).

    You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run ‘python manage.py migrate’ to apply them.

    December 11, 2017 – 05:54:12
    Django version 2.0, using settings ‘mysite.settings’
    Starting development server at http://192.168.56.102:8000/
    Quit the server with CONTROL-C.
    Invalid HTTP_HOST header: ‘192.168.56.102:8000’. You may need to add ‘192.168.56.102’ to ALLOWED_HOSTS.
    [11/Dec/2017 05:54:22] “GET / HTTP/1.1” 400 61463
    Invalid HTTP_HOST header: ‘192.168.56.102:8000’. You may need to add ‘192.168.56.102’ to ALLOWED_HOSTS.
    [11/Dec/2017 05:54:23] “GET /favicon.ico HTTP/1.1” 400 61445
    ^C

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ vim /home/krishna/DjangoProjects/FirstProject/envib/python3.5/site-packages/django/http/request.py
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ python manage.py runserver 192.168.56.102:8000 Performing system checks…

    System check identified no issues (0 silenced).

    You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run ‘python manage.py migrate’ to apply them.

    December 11, 2017 – 05:56:05
    Django version 2.0, using settings ‘mysite.settings’
    Starting development server at http://192.168.56.102:8000/
    Quit the server with CONTROL-C.
    [11/Dec/2017 05:56:09] “GET / HTTP/1.1” 200 16559
    [11/Dec/2017 05:56:09] “GET /static/admin/css/fonts.css HTTP/1.1” 200 423
    [11/Dec/2017 05:56:09] “GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1” 200 80304
    [11/Dec/2017 05:56:09] “GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1” 200 82564
    [11/Dec/2017 05:56:09] “GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1” 200 81348
    [11/Dec/2017 05:56:44] “GET /admin HTTP/1.1” 301 0
    [11/Dec/2017 05:56:44] “GET /admin/ HTTP/1.1” 302 0
    [11/Dec/2017 05:56:44] “GET /admin/login/?next=/admin/ HTTP/1.1” 200 1855
    [11/Dec/2017 05:56:44] “GET /static/admin/css/base.css HTTP/1.1” 200 16106
    [11/Dec/2017 05:56:44] “GET /static/admin/css/responsive.css HTTP/1.1” 200 17894
    [11/Dec/2017 05:56:44] “GET /static/admin/css/login.css HTTP/1.1” 200 1203
    [11/Dec/2017 05:57:14] “GET / HTTP/1.1” 200 16559
    ^C(env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ cat polls/admin.py from django.contrib import admin

    # Register your models here.
    from .models import Question, Choice

    admin.site.register(Question)
    admin.site.register(Choice)

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ cat polls/models.py
    from django.db import models

    # Create your models here.

    class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField(‘date published’)

    def __str__(self):
    return self.question_text

    class Choice(models.Model):
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)

    def __str__(self):
    return self.choice_text

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ python manage.py makemigrations polls
    Migrations for ‘polls’:
    polls/migrations/0001_initial.py
    – Create model Question
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ python manage.py makemigrations polls
    Migrations for ‘polls’:
    polls/migrations/0002_choice.py
    – Create model Choice
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ python manage.py migrate
    Operations to perform:
    Apply all migrations: admin, auth, contenttypes, polls, 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 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 polls.0001_initial… OK
    Applying polls.0002_choice… OK
    Applying polls.0003_choice_votes… OK
    Applying polls.0004_delete_choice… OK
    Applying polls.0005_choice… OK
    Applying sessions.0001_initial… OK
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$

    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$ python manage.py createsuperuser
    Username (leave blank to use ‘krishna’): admin
    Email address:
    Password:
    Password (again):
    Superuser created successfully.
    (env) krishna@ubuntu:~/DjangoProjects/FirstProject/mysite$

Leave a Reply

Your email address will not be published. Required fields are marked *