Django for beginners

Hello,

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 setup.py . 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 setup.py 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

django-admin.py 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:

manage.py

__init__.py

settings.py

urls.py

Before I get into how to setup the settings, I will give a brief about what the files are.

__init__.py is there to tell python that evry subdirectory uunder this directory should be part of sys.path

manage.py is like a linker file, which links tells python to look for the settings in the file called settings.py

settings.py : 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.

urls.py : 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 settings.py file

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

The first line of the settings file conatins  the following line

DEBUG=True

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

ADMINS = ()

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.

DATABASES = {
‘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.

TEMPLATE_DIRS = (

‘/templates’

)

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 .