Intro to Django

Intro to Django – Building a To-Do List

Django is a powerful web framework, created in Python, which follows the DRY (Don’t repeat yourself), and batteries included philosophies. It allows for rapid website development by providing a wide range of tools and shortcuts out of the box. Django is extremely fast and flexible – even faster than all of the PHP frameworks available. In this article, I’ll introduce you to Django by showing you how to build a simple to-do list.

Why Django? Why Not X?

As stated above, Django follows the batteries included philosophy. PHP frameworks such as Code Igniter try and strip all of the “unnecessaries” out for maximum performance; but Django can include all of these out of the box and still be high performance because of its modular design (If you don’t call include feature XX then it isn’t included; so it doesn’t slow the script down). Here are some of the most notable features:

  • User/Authentication system
  • Automatic RSS generation
  • GIS
  • Automatically generated Admin interface
  • Generic views (more on this later)
  • Three level caching system
  • Powerful template system
  • Comments

MTV

Django runs on an MTV (Model Template View) system (Not the television channel). This is different from MVC.

  • Model. The model sets out the schema for our database. Unlike PHP frameworks, you don’t write your queries here. Using Django’s ORM you declare the fields, field types and any additional data such as Meta information.
  • View. In the View, you set all of your code logic. You may need to pull some results from the database or manipulate some strings. The view expects a request and a response. The response is typically an http redirect, http error (404), or a call to load a template. When loading a template you usually pass some data through – most likely, results from a database query.
  • Template. The template is the plain HTML code with Django’s custom Template ‘language’ (similar to smarty) in it. You look and iterate just like you would with any other language.

Creating Our To-Do List

We are going to create a very simple application, a to-do list, to showcase how to get started with Django. We’ll use the automatically generated admin interface to add, edit and delete items. The ORM will be used to query the database. The view will pull get the to-do items and pass them onto the template where they are shown. I’m going to assume you have Django installed and running.

First, we need to create our project. To do this, we need to use the command line.

django-admin.py startproject todo

That should have created a directory called todo. Go into that directory. (cd todo)

manage.py startapp core

Yours should look similar to the image below.

Now that we have created the project, we need to input some settings. Open up todo/settings.py with your favorite text editor. Django supports a wide range of databases – MySQL, Postgres and Oracle, to name a few. Since we are only developing, we are going to use SQLite. SQLite only requires the name of the database to be specified.

Change Line 12 to:

DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.

Change Line 13 to:

DATABASE_NAME = 'to-do.db'             # Or path to database file if using sqlite3.

With the database set up. we only need to do a couple more edits in the settings file. On line 69, we need to set a full path to the templates directory where all of our HTML templates will be stored. Create the directory in /todo/core/ and then specify it on line 69. My template directory and installed apps tuple:


Finally, we need to add our application and the admin application into the INSTALLED APPS tuple. So on line 81, insert:

'django.contrib.admin',
'todo.core',

After

'django.contrib.sites',

That’s it for the settings.py file.

Model

Now we need to specify our model. Open /todo/core/models.py with your text editor. As I said above, models define the structure of the database. There’s no need to run raw CREATE TABLE.. SQL queries, Django can do all of this for us. We just need to specify what we want. Since its a to-do list – each to-do will have a title, some content, and the date it was published. It’s very easy to represent this in Django.

from django.db import models

class todo(models.Model): #Table name, has to wrap models.Model to get the functionality of Django.
		
	name = models.CharField(max_length=100, unique=True) #Like a VARCHAR field
	description = models.TextField() #Like a TEXT field
	created = models.DateTimeField() #Like a DATETIME field

	def __unicode__(self): #Tell it to return as a unicode string (The name of the to-do item) rather than just Object.
		return self.name

Now that we have defined the database, we need to tell Django to sync all of the tables. We can do this by calling the syncdb command in the command line. Open up the command line and navigate to todo/.

manage.py syncdb

You should get some output for the tables created, as well as a prompt for creating a super user. Create the user and make a note of the username and password, as we’ll use them to login to the admin area. For this tutorial we are going to use Django’s test server to test our application. It is strongly advised that you do not use the test server in deployment, but instead use apache + mod_python or FastCGI. To start the test server, we again call manage.py but with the argument of runserver. We can additionally provide a port for it to run on. By default it runs on port 8000.

manage.py runserver 9090

To view the results we can go to http://127.0.0.1:9090/ .

todo/urls.py

Now we need to set up our URLs. Unlike PHP all URLs in Django are routed. That simply means we specify them ourselves. If a user tries to navigate to a page that isn’t set, it simply returns a 404. Since this is a simple to-do application, we only need to set one URL, the index page. We are also going to enable our admin panel so that we can add, delete and edit our to-do items.

from django.conf.urls.defaults import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/(.*)', admin.site.root), #Lets us access the admin page
    (r'^$', 'todo.core.views.index'), #Our index page, it maps to / . Once the page is called it will look in /todo/core/views.py for a function called index
    
)

The final thing we need to do before we can use the admin panel is to register the model with it. Create todo/core/admin.py and set the contents to:

from django.contrib import admin #Import the admin
from models import todo #Import our todo Model.

admin.site.register(todo) #Register the model with the admin

Now that the URLs are set, the database is synced, and the model is registered, we can access our administrator panel. Start the test server if it isn’t already and navigate to http://127.0.0.1:9090/admin/ . Login with the super user details you created.

You can see under the core heading there is “todo” – the name of the model. By clicking on this, we can see all of the “to-dos” we have made. The admin interface for making new to-dos is below.

Now that creating, updating and deleting the to-do items are finished, all we need to do is display them. If you navigate to http://127.0.0.1:9090/ you will see an error saying that “the view called index does not exist”. Remember – in the urls.py file, we specified that if the user visited the index, then it would load /todo/core/views.py with the function index()? We’re going to specify that now. We write index() just like any other Python function, except it has to accept a request and return a response.

from models import todo
from django.shortcuts import render_to_response

def index(request): #Define our function, accept a request

    items = todo.objects.all() #ORM queries the database for all of the to-do entries.

    return render_to_response('index.html', {'items': items}) #Responds with passing the object items (contains info from the DB) to the template index.html

Now all that’s left is to create our template in the todo/core/templates/ directory. In this template, we want to iterate through all of the items (in the object items) and display them on the page. All of the Django logic is wrapped with curly braces; if it’s a variable, then it is wrapped with two curly braces.


<h2>todo</h2>

{% for x in items %}
<p>{{ x.name }} - {{ x.created|date:"D d M Y" }}</p>

<p>{{ x.description }}</p>
<hr />
{% endfor %}

You’re finished! Add a few to-do items in the admin panel, and then go to http://127.0.0.1:9090 . Your layout should look similar to the image below.

Further Reading

I encourage you to learn more about Django if you’re intrigued. Feel free to tweet me with any questions that you might have.

Tags: django
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.jashsayani.com Jash Sayani

    Making something like Remember The Milk would be really cool !!

    BTW, they use the jQuery interface.

  • http://rlog.cn Robin

    this is a good expample to django dev.

  • http://chrisodonnell.name/ Christopher Francis O’Donnell

    Nice. Finally a Django tutorial for the masses, put in front of the right audience. I’d also recommend “Python Web development with Django” to any other Django newcomers. It’s a good read.

  • http://www.jsws.com.au/wollongong-web-design Wollongong Web Designer

    I agree with Jash Sayani … creating something like remember the milk would be awesome!

  • Pontus Johansson

    Finally a helpful Django tutorial!

  • http://www.xqlusive.nl xQlusive

    Interesting system, thanx for the tut.

  • ger

    Please, improve this intro with a jquery integration

  • Mark

    Nice

  • http://www.medinheaven.co.uk/ Phillip

    If you are going to buy any books, ensure they are based on Django 1.0 and not the older 0.96.

    Phillip.

  • http://simonzimmermann.com Simon Zimmermann

    Thanks for the article on Django. I discovered this framework some time ago. Really impressed me. I guess my only problem is the Python syntax. I really never liked those “:” everywhere :p Other then that “python & django > php”

  • http://www.image2markup.com berofx

    Being a n00b that I am, is there a Python version of XAMPP (Apache + MySQL + Python)?

  • Suman Shakya

    great article. I would also like to read tutorials on TYPO3 CMS. Its quite popular here in Europe.

  • M.A.Yoosuf

    cool.

    NETTUTS is moving to all the programming Languareg……huraaaaaa

  • http://blog.insicdesigns.com insic

    I heard Django often but nothings motivate me to try it. But after reading this tutorial. I have my first django app running. nice tutorial. :)

  • http://www.darrenmcpherson.co.uk/ Darren McPherson

    I hear nothing but great things about Python and Django. At present I’m using PHP + Code Igniter.

  • http://dantar.multiply.com dantario

    yes yes yes

    this is the python revolution. good by to PHP, well come Python.

  • http://www.patternhead.com Patternhead

    How about a step by step guide to getting this running on Google App Engine?

  • http://www.kevinquillen.com Kevin Quillen

    “good by to PHP, well come Python.”

    That kind of crap always makes me laugh.

    Are there any tools that would automate setting up a Django site instead of cmd line’ing it each time?

  • http://www.freshclickmedia.com Shane

    Nice to see something different on NETTUTS.

    I’m not going to rush to look at Django though – none of the ‘Why Django’ points seem that compelling to me. It just seems like another framework, Python might be ‘cool’, but what compelling reasons are there to learn it when other frameworks out there, based on more established languages such as PHP do the job?

  • Wassim

    Am I the only one experiencing problems to post a comment today or..?!

  • Josh

    I’ve been wanting to learn Django, so I was glad to see this article. Two things though:

    First, is anyone else a little confused about the difference between MVC and MTV? From my reading of the tutorial, it sounded pretty similar, only MTV renames ‘controller’ to ‘view’ and ‘view’ to ‘template’. What am I missing?

    Second, the article seems to imply that you can’t use ORM in PHP Frameworks. But several PHP frameworks, such as Kohana (http://kohanaphp.com) have ORM built in, and most of the others have ways that you can add it as a plug-in.

  • http://blog.brenelz.com Brenelz

    Nice I have been interested in DJANGO but haven’t had time to play around in it much….

  • http://smilingdesigner.com Bjorn

    Great… just when I’m really getting into PHP…

  • http://www.pixmatstudios.com demogar

    Finally a Django tutorial!
    I’m a big fan of CodeIgniter (PHP Framework) but I really prefer Django all over.
    Great tut!

  • http://satedproductions.com Michael Thompson

    @Patternhead: GAE has a different ORM, and different model definitions so your best bet is to get comfy with Django/Python and then learn the ins and outs of BigTable and the rest of GAE.

    Also, why not add a “complete” field to the model?

    complete = models.BooleanField()

  • http://satedproductions.com Michael Thompson

    Also, to the PHP trolls: don’t get so defensive. Python is better, and Djanog’s ORM and MVT (still technically MVC) is cleaner. :P

  • http://www.kevinquillen.com Kevin Quillen

    How is it ‘better’? I’m not a PHP troll, no ones proven how Python is ‘better’ than PHP.

    So just because you say so, our entire company should switch from PHP to Python overnight?

    • Leon Waldman

      - It is clean. The code layout was done with readability in mind.
      - It is closer to the OS.
      - It is infinitely more modular.
      - Easy to deploy, and… scale.
      - Short learning curve.
      - OO frow the ground…
      - etc…

  • http://www.reycode.com Michael Rice

    Sweet Django tutorial!

  • Jeremy

    Nice to see a django tutorial finally, I was close to putting something together myself!

    @Kevin – I think the push towards frameworks like django or rails and the accompanying remarks that they’re *better* than PHP are more of a performance comment. Though its nearly impossible to put together a fair test to benchmark frameworks against one another, the attempts that I have seen show frameworks like django and rails on top of the competition. In some cases the differences in performance are fairly insignificant.

    I’ve also seen some great studies between rails and django on plain development and deployment time for identical projects. Because django comes with a built-in admin interface it typically comes out on top as far as time goes.

    I almost started to get into PHP and kept reading articles from noteworthy professionals in the field saying to stay away and move towards django or rails. I’m glad I listened.

  • Pingback: Heads Up Seven Up Blogroll for February 2nd, 2009 | GrandmasterB dot com

  • http://rajaseelan.com Raja

    Its great to see a django tutorial. Nice to see you guys moving into python stuff.

  • Dimitris

    Thanks. I hope there will be a sequence to this tutorial!

  • http://www.lastkarrde.com Logan
    Author

    Thanks alot for the feedback guys, I’m glad you liked the tutorial.

    Phillip has a very good point about Django 1.X being backward incompatible with 0.96. If your going to buy a book I’d recommend ‘Python Web Development with Django’. It’s very detailed, great for someone who is just getting started with Python/Django.

    berofx: There is no need for a XAMPP alternative for Django as there is the ‘runserver’ built in for testing your project locally.

    Patternhead: Google say that Django works on app engine when infact there are alot of features missing from a standard Django install. I may write an App Engine tutorial later though : ) .

    KevinQuillen: A frontend for the commandline commands in Django would only be used on the desktop. When you deploy the project to your server you would be forced to use the command line anyway. It’s good to get used to it. There are alot more commands you can use with manage.py than I showed such as generating a SQL schema of your database.

    Shane: It’s a large subject to go over in one comment. Read http://news.ycombinator.com/item?id=314143

    Josh: Yeah, they’ve essentially renamed things. More http://tinyurl.com/3mwwhf . Django ORM is alot more flexible, I was trying to hint (and didn’t get across) that most mainstream PHP frameworks don’t have “correct” models. They are just a page where you write SQL queries rather than design your database. Python frameworks, Ruby frameworks and Symfony (to a degree) have “correct” models.

  • Jake

    So… If I am looking to invest a good amount of time in learning one of the two, which should I learn… PHP or Python (Django)??

  • Matt

    For books, I would wait. The definitive guide to django is being re-written and available online at djangobook.org. James is also rewriting his book practical django projects which is also really good.

  • http://users.ecs.soton.ac.uk/mp1206 Matt Porter

    This is a nice tutorial, any chance of a follow up to add an interface for adding items to the list?

  • http://www.lastkarrde.com Logan
    Author

    Matt Porter: There is the admin interface there. Do you want another/non authenticated one?

  • http://masonsklut.com Mason Sklut

    The Django “To-Do list” app has been a classic for as long as I’ve known :-)

  • Pingback: Daily Links | AndySowards.com :: Professional Web Design, Development, Programming, Hacks, Downloads, Math and being a Web 2.0 Hipster?

  • http://jkb.name/ Jonas

    Jeremy wrote: “I think the push towards frameworks like django or rails and the accompanying remarks that they’re *better* than PHP are more of a performance comment. Though its nearly impossible to put together a fair test to benchmark frameworks against one another, the attempts that I have seen show frameworks like django and rails on top of the competition.”

    Do you have a link to the bechmarks you’re mentioning? Because from what I’ve read, both PHP and Django are known for being fast even under high load (e.g., Facebook uses PHP), whereas Rails is notorious for being slow and unsuited for high-traffic sites.

    I’m a PHP programmer, and while I’m currently looking into Django, it’s definitely not because of speed issues with PHP. I’ve used PHP for sites that get millions and millions of hit, and there were no problems whatsoever with the speed of PHP. The RDBMS was the bottleneck.

  • http://www.contempographicdesign.com Chris Robinson

    great to see a Django tut, hope to see more in the future

  • Joe

    Shane (“based on more established languages such as PHP do the job?”):
    You do know Python is much older and more established then php, right? Python development started in the 80′s, php around 94. If you were referring to Django, then the proper comparison would be CakePHP, Codeigniter, Zend to Django..

  • http://www.1pixelbrush.com Dan

    Looks interesting, may give it a go

  • http://chrisodonnell.name/ Christopher Francis O’Donnell

    @ger

    This isn’t a jQuery tutorial. It isn’t a tutorial about how to replicate RTM. It’s supposed to be a basic into to Django, a Python framework, not Javascript library.

    That said, there are some nice jQuery tutorials on NETTUTS, as well as learningjquery.com, if you want to check those out. :-)

  • http://chrisodonnell.name/ Christopher Francis O’Donnell

    @Jake

    I’d go with Django, but if you want a job at Facebook in the near future, PHP. But who wants to work at Facebook? :-)

  • Pingback: links for 2009-02-04 « pabloidz

  • http://rajaseelan.com Raja

    Well, PHP based applications are more ‘popular’, plus I think as far as commercial hosting goes, almost everyone supports PHP.

    Python is picking up thanks to Google App Engine & Django.

    DJango also requires a different approach to hosting, so it may take awhile before the el-cheapo webosts offer it, or go bug the CPanel authors ;)

  • http://www.kevinquillen.com Kevin Quillen

    “if you want a job at Facebook in the near future, PHP. But who wants to work at Facebook?”

    I hope you’re kidding. Making an underlying comment that PHP is simply dead outside of Facebook is ridiculous.

    Heres an idea. Everyone grasp the concepts of programming and problem solving instead of memorizing syntax and claiming X language is dead, long live Y language.

    Trends come and go. Like it or not PHP and .NET are going to be the dominant development languages on the internet for years to come. So just because Python has been around since the 80s doesn’t mean that if a magical framework comes along that suddenly all else is obsolete.

  • http://lastkarrde.com Logan
    Author

    cPanel don’t officially support Django. That being said, if your aiming to run a good website, your going to want it on a VPS. It’s just as easy to set up a Django stack as it is to set up a PHP stack.

  • http://henriklied.no Henrik Lied

    Regarding PHP vs. Python: The amount of strange decisions made by the PHP core team lately shows that communication between the users and the developers of PHP is pretty bad.

    I know a lot of developers who are completely dependent on PHP for their day job, but they have no interest in jumping on the PHP6 bandwagon, because of the weird changes to syntax and form.

    Regarding installation of Python/Django on simple hosts: I’ll give you one clue to why it’s not a cPanel-ready solution: World Online.
    Django originated from a news organization, and believe me – you don’t run a huge news site on a shared server with no root access.
    Having to use shell / SSH to configure your site is a necessity for larger sites anyway. How do you configure munin, apache, memcached and postgresql in a fair way through cPanel? You don’t.

    And yes, PHP is slow when compared to Python. So is Ruby. But I don’t mind. PHP’s performance is good enough for Facebook (although Facebook only uses PHP for the front end stuff. Most of the backend is written in Erlang and other pre-compiled languages).

    What I like about Python is the huge toolbox it brings. I don’t have to jump to Perl or another language to do server specific operations. In PHP you do.

    As most other developers using Python, I come from a PHP background. Once you get off, you’ll probably never go back.