How to start building websites on Linux with Django

Maxim Dzhuliy
02-06 12:45
  1. Web Development
  2. Python

How to start building websites on Linux with Django

Django is a web framework for python. It provides you with the basic components that are required to build your website like HTML templates, User Authentication, Web Server etc.
In this tutorial I will describe how you can install Django 1.7 on Linux with the latest version of Python and PostgreSQl.

Software Versions
Linux(CentOS 6.5)
Python 3.4
Django 1.7
PostgresSQL 9

Step 1: Install Python 3.4 on your Linux machine.
Ubuntu/Mint/Raspberry Pi users can skip this step as these distro’s generally have the latest versions of python (check by running “python –version”)


[leo@django~]$ wget
[leo@django~]$ tar -xvzf Python-3.4.2.tgz
[leo@django~]$ cd Python-3.4.2
[leo@django~]$ ./configure
[leo@django~]$ make
[leo@django~]$  sudo make altinstall
[leo@django~]$   python --version
Python 3.4.2

Step: 2 Install Django on Linux

Before you install Django create a virtual environment, where the project dependencies will be installed and will not conflict with system dependencies.


[leo@django~]$ pyvenv-3.4 venv3.4
[leo@django~]$ source venv3.4/bin/activate
[leo@django~]$ pip install django

Step 3: Install PostgreSQL 9.4 on Linux

Django recommends the use of PostgreSQL for it’s database operations. If you are not comfortable with Postgres, then you can configure MySQL or you can leave the settings to default and use SQLlite as well. Ubuntu/Mint/RasberryPi users can follow the PostgreSQL installation guide at


[leo@django~]$ sudo vim /etc/yum.repos.d/CentOS-Base.repo
#add the below line to the base and update sections
[leo@django~]$ sudo yum localinstall
[leo@django~]$ sudo yum install postgresql94-server postgresql94-contrib postgresql-devel
[leo@django~]$ sudo service postgresql-9.4 initdb
[leo@django~]$ sudo service postgresql-9.4 start
Create a Database and assign a user to it.


 [leo@django~]$ su - postgres
 [leo@django~]$ createdb yourdbname
 [leo@django~]$ createuser -P yourdbuser   #createuser will prompt you for a password, enter it twice.
 [leo@django~]$ psql
 [leo@django~]$ postgres=#GRANT ALL PRIVILEGES ON yourdbname TO yourdbuser;
 [leo@django~]$ \q
Now try to connect via the command prompt


[leo@django~]$ psql -U yourdbusername -h localhost -W

After you enter you password you will get the below error
“FATAL: Ident authentication failed for user”

This is because by default PostgreSQL uses ‘ident’ authentication i.e it checks if the username exists on the system. You need to change authentication mode to ‘trust’ as we do not want to add a system user.
Modify the settings in "pg_hba.conf" to use 'password' authentication.


[leo@django~]$ sudo  vim /var/lib/pgsql/9.4/data/pg_hba.conf
host    all             all               password
# IPv6 local connections:
host    all             all             ::1/128                 password

[leo@django~]$ sudo service postgresql-9.4 restart
Install the PostgreSQL driver


[leo@django~]$ pip install psycopg2
Step 4: Create your first Django website


[leo@django]$ startproject website
[leo@django]$ cd website
[leo@django website]$ vim website/

    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'yourdbusername',
            'PASSWORD': 'yourdbpassword',
            'HOST': 'localhost',                      
            'PORT': '',

[leo@django website]$ python website/ syncdb #Enter a admin user name and password for Django
#Run the server
[leo@django website]$ python website/ runserver

You should be able to access the site at the url and the admin panel at
install django 1.7 on linux


To add your own index template add the following code to


[leo@django website]$ vim website/
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Create a templates folder and add your index.html file in it along with the default admin templates.


[leo@django website]$ mkdir templates
[leo@django ~]$ cp -rv venv3.4/lib64/python3.4/site-packages/django/contrib/admin/templates/*  website/templates/
[leo@django~]$vim website/index.html
<p> Hi this is Leo and This is my first website on Linux </p>
Finally modify your file to look as follows.


[leo@django~]$ vim website/website/

from django.conf.urls import patterns, include, url
from django.contrib import admin
from django.views.generic.base import TemplateView

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'blog.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(,
    url(r'^$', TemplateView.as_view(template_name='index.html'), name="home"),
Now run your server


[leo@django website]$ python website/ runserver
Performing system checks...

System check identified no issues (0 silenced).
February 02, 2015 - 10:44:18
Django version 1.7.4, using settings 'website.settings'
Starting development server at
Quit the server with CONTROL-C.
[02/Feb/2015 10:44:21] "GET / HTTP/1.1" 200 1759

Now you can design your own templates or you can use pre-built ones from

Here are some references to help you design your own template.

Original source