Django for beginners


This is my first blog post and I want to dedicate this to all those web developers out there , who want to move to django for backend developement.

This post contains details of installing django and setting up the basic settings to get started with. For all those who are wondering what is django, please look up here.

Prerequisites :

1) This post contains the details of installing django on a linux machine. For windows and mac users, the installation procedure might slightly vary. However the setting up of django settings, once the installation is done remains the same.

2) The post is written with the assumption that the user is familiar with basic python. For tutorials on python, please look up here.

Installation Procedure:

Now that we are done with the talking lets get our hands wet. To install django, download the tarball of the official release here .

Please make sure you download the official release from the link in topic 1 of the page mentioned above. Once the tar is downloaded, extract it to a suitable folder of you choice. In my example , I am extracting it to /home/abinav.

To extract the tarbal, run the following command on the terminal:

tar -zxvf Django-1.3.1.tar.gz

Once the folder is extracted, change the working directory into Django-1.3.1 from terminal(command to do that is listed below)

cd Django-1.3.1

Inside this folder, you can notice that there is a script called . To install django, you have to run this script. Type the following command on the terminal(This requires python to be installed on your computer. You can download it from here.)

python install

Thats it!! Now django would be installed on your computer.

Creating a new Project

Now that django is installed successfully, we shall look at how to create a new project and set up the settings.

To create a new project, cd into the folder where you want the project files to reside and run the following command startproject projectname

You can replace the projectname with the suitable project name you want. For this post I will use the name ‘projectname’ through out.

This will create a new directory in your working folder, with the name projectname .

This directory will have the following files:

Before I get into how to setup the settings, I will give a brief about what the files are. is there to tell python that evry subdirectory uunder this directory should be part of sys.path is like a linker file, which links tells python to look for the settings in the file called : This is the most important file which contains the settings of your project. We shall look at how to put the settings in the next paragraph. : This file contains the urls your project will need to look up and the views it needs to connect to when hit on that url.

Setting up the file

Now that we have a basic idea about the files, let us look at how to set up a basic file.

The first line of the settings file conatins  the following line


This is to tell django that this is in development stages. So django will provide detailed errors of what went wrong, whenevr a 500 or 404 error is encountered. Once the entire developement is done change this to

DEBUG = False

and the conventional 404 and 500 pages will be displayed.For this post we will work with DEBUG = True .

Next line contains


Fill in the name and email adress , so that django will mail the details of a 500 error whenever it is in DEBUG=False mode. For now we will leave it blank.

Next comes the major part of the settings file.

‘default’: {
‘ENGINE’: ‘django.db.backends.’, # Add ‘postgresql_psycopg2′, ‘postgresql’, ‘mysql’, ‘sqlite3′ or ‘oracle’.
‘NAME’: ”, # Or path to database file if using sqlite3.
‘USER’: ”, # Not used with sqlite3.
‘PASSWORD’: ”, # Not used with sqlite3.
‘HOST’: ”, # Set to empty string for localhost. Not used with sqlite3.
‘PORT’: ”, # Set to empty string for default. Not used with sqlite3.

This contains the details of how django should connect with the database.

‘ENGINE’ : specifies which kind of database to use. For this post we will use mysql database.

Hence,  it should be

‘ENGINE’ : ‘django.db.backends.mysql’

The next line contains the name of the database to use. For this post I will use the database name , ‘projectname_db’.So the line should look like :

‘NAME’ : ‘projectname_db’

The next to lines gives the details of the username and password for connecting to the mysql database. For this post we will assume, the username is ‘user’ and pasasword is ‘password’

‘USER’: ‘user’

‘PASSWORD’ : ‘password’

The last line tells a specific posrt to use while connecting to mysql server. For now we will leave it blank and let mysql use the default port.

the line after DATABASES contains information about timezone and characterset. For now we will use the default values.

The two lines after this are fairly important. They tell django where to look for the media folders which the project uses.

If you are testing it on your computer, put the media files(js,css,images) in the folder /var/www and mention the path in MEDIA_ROOT as follows:

MEDIA_ROOT = ‘/var/www/’

and mention the url as

MEDIA_URL = ‘http://localhost/’

Similarly, static root and static url tell django where to look for static pages. For now we will leave it blank and let django assume that all the pages used are dynamic.

Next bunch of lines give various description of the project, which for now we will ignore.

The next important thing is template directory. Templates are the html files which django backend serves. You can read more about them here.

So create a folder called ‘templates’ within your project folder and include this line in the settings.




Once all this is done, you are now ready to go. You can write your views , models and mention the urls.

You can view the entire settings file here .

About these ads
  1. studd man! You should continue this and make it a complete django tutorial! :)

  2. Great tutorial for a Django newbie like me. Thanks a lot!

  3. You are welcome!!

  4. Dude you should really use

     tags for code snippets.
    • Ok. I shall do that from my next post. Thanks for the response!!

      • Do you use emacs? Then you might find useful. :P

        Apart from that, nice post! Keep the fundae putting enthu up!!

  5. Sujeet Gholap :
    Your comment is awaiting moderation.
    Do you use emacs? Then you might find this useful.
    Apart from that, nice post! Keep the fundae putting enthu up!!

    • vivek
    • February 21st, 2012

    nice one..waiting for others ;)

    • moss
    • March 18th, 2012

    Where is next part?

  6. @moss: Really sorry. Got stuck with course work. Will soon post the next tutorial. Thank you for the patience !!

    • Nice blog! But when can I expect the next tutorial? Moss also must be waiting eagerly like I am.

  7. Thanks a lot for your comments !! Yes, the next part is almost ready. you can expect it anytime soon :)

  8. Nice tutorial.One doubt. Why you are mentioned django as back-end development. Is it a web framework like struts or something else?thx..

  1. December 3rd, 2013
You must be logged in to post a comment.

Get every new post delivered to your Inbox.

%d bloggers like this: