Django Tutorial
Django Tutorial
Audience
This tutorial is designed for developers who want to learn how to develop quality web
applications using the smart techniques and tools offered by Django.
Prerequisites
Before you proceed, make sure that you understand the basics of procedural and object-
oriented programming: control structures, data structures and variables, classes, objects,
etc.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com.
i
Django
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
2. DJANGO – OVERVIEW.......................................................................................................... 3
3. DJANGO – ENVIRONMENT................................................................................................... 4
Create a Project....................................................................................................................................... 7
ii
Django
Filters .................................................................................................................................................... 24
Tags ....................................................................................................................................................... 24
iii
Django
20. COMMENTS....................................................................................................................... 69
iv
1. DJANGO – BASICS Django
Django is a high-level Python web framework that encourages rapid development and clean,
pragmatic design. Django makes it easier to build better web apps quickly and with less code.
Note: Django is a registered trademark of the Django Software Foundation, and is licensed
under BSD License.
History of Django
2003: Started by Adrian Holovaty and Simon Willison as an internal project at the
Lawrence Journal-World newspaper.
2005: Released July 2005 and named it Django, after the jazz guitarist Django
Reinhardt.
Current: Django is now an open source project with contributors across the world.
Loosely Coupled: Django aims to make each element of its stack independent of the
others.
Don't Repeat Yourself (DRY): Everything should be developed only in exactly one
place instead of repeating it again and again.
Clean Design: Django strictly maintains a clean design throughout its own code and
makes it easy to follow best web-development practices.
Advantages of Django
Here are few advantages of using Django which can be listed out here:
5
Django
Framework Support: Django has built-in support for Ajax, RSS, Caching and various
other frameworks.
6
2. DJANGO – OVERVIEW Django
As you already know, Django is a Python web framework. And like most modern framework,
Django supports the MVC pattern. First let's see what is the Model-View-Controller (MVC)
pattern, and then we will look at Django’s specificity for the Model-View-Template (MVT)
pattern.
MVC Pattern
When talking about applications that provides UI (web or desktop), we usually talk about MVC
architecture. And as the name suggests, MVC pattern is based on three components: Model,
View, and Controller. Check our MVC tutorial here to know more.
The following diagram illustrates how each of the components of the MVT pattern interacts
with each other to serve a user request:
The developer provides the Model, the view and the template then just maps it to a URL and
Django does the magic to serve it to the user.
7
3. DJANGO – ENVIRONMENT Django
Django development environment consists of installing and setting up Python, Django, and a
Database System. Since Django deals with web application, it's worth mentioning that you
would need a web server setup as well.
If you're on one of the latest Linux or Mac OS X distribution, you probably already have Python
installed. You can verify it by typing python command at a command prompt. If you see
something like this, then Python is installed.
$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Otherwise, you can download and install the latest version of Python from the link
http://www.python.org/download.
You can download the latest version of Django from the link
http://www.djangoproject.com/download.
You can use the package manager of your OS, or use easy_install or pip if installed.
Install it manually using the official archive you downloaded before.
We will cover the second option as the first one depends on your OS distribution. If you have
decided to follow the first option, just be careful about the version of Django you are installing.
8
Django
Let's say you got your archive from the link above, it should be something like Django-
x.xx.tar.gz:
$ django-admin.py --version
If you see the current version of Django printed on the screen, then everything is set.
Note: For some version of Django it will be django-admin the ".py" is removed.
Windows Installation
We assume you have your Django archive and python installed on your computer.
On some version of windows (windows 7) you might need to make sure the Path system
variable has the path the following C:\Python27\;C:\Python27\Lib\site-
packages\django\bin\ in it, of course depending on your Python version.
c:\>cd c:\Django-x.xx
Next, install Django by running the following command for which you will need administrative
privileges in windows shell "cmd":
To test your installation, open a command prompt and type the following command:
c:\>django-admin.py --version
If you see the current version of Django printed on screen, then everything is set.
OR
9
Django
c:\> python
>>> import django
>>> print django.get_version()
10
Django
11