Django is a Python web framework designed to encourage loose coupling and strict separation between pieces of an application. These pieces are: data access logic, business logic, and presentation logic. Together they comprise a concept that's called the Model-Template-View (MTV) pattern of software architecture:

Setting up a Django Project

  1. Create a new Django web project called sigcse. Type at the command line: startproject sigcse

    This creates a new folder named sigcse. Change to that directory with the following command line:

    cd sigcse

    This folder should contain the following four files:

    File Name Description Used to indicate that the current folder is a Python package. Utility script that allows you to interact with the current Django project in various ways. Configuration file for this Django project. The URL mappings for this Django project.
  2. Run the web server to see if it works correctly. Type at the command line:

    python runserver

    Now, visit http://localhost:8000/ with your Web browser. You should see a welcome page.

  3. Open Komodo Edit and create a new Komodo project. From the main menu select Project/New Project..., in the dialog window type sigcse.komodoproject as the name of the project, navigate to the sigcse directory, and press the Save button.
  4. Create a folder for your web template pages. In the Places sidebar use the secondary mouse button to bring up the context menu. Select the New Folder... option, type templates in the dialog window and press the OK button.
  5. Tell Django the location of your templates folder. Open and edit the file. Add the highlighted lines:

    import os.path
        # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
        # Always use forward slashes, even on Windows.
        # Don't forget to use absolute paths, not relative paths.
        os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
  6. Create a base.html file in the templates folder. Every HTML file that we create will extend from this file. The contents of this file should be:

    <!DOCTYPE html>
        <meta http-equiv="Content-Type" 
              content="text/html; charset=UTF-8">
        <style type="text/css">
            body {
                color: #efd;
                background: #453;
                padding: 0 2em;
                margin: 0;
                font-family: sans-serif;
                font-size: 120%;
            h1 {
                padding: 30px 30px;
                background: #675;
                color: #ffa;
                font-size: 150%;
            h2 {
                color: #bf8;
                border-top: 1px dotted #fff;
                margin-top: 2em
            p {
                margin: 1em 0
            a:link {
                color: #fe5;
            a:visited {
                color: #fe5;
            a:hover {
                color: #fe5;
            hr {
                margin: 20px 0px;
                color: #efd;
                background-color: #efd;
                height: 2px;
            .footer {
                font-size: 90%;
                color: #ffa;
                text-align: center;
        <title>Django Worshop</title>
        {% block main %}No body!{% endblock %}
        <p class="footer">
            Workshop 6: <em>Web Development with Python and Django</em>
            <strong>SIGCSE 2011</strong>        
  7. Create a new application called examples. Open a new command line window and move to the sigcse directory once again. At the command line type:

    python startapp examples
  8. Create a foo.html file in the templates folder. The contents of this file should be:

    {% extends "base.html" %}
    {% block main %}
        Yes! This is my first Django Web page.
    {% endblock %}
  9. Create a view that sends information to the template. Open and edit the file examples/ so that it has the following content:

    from django.shortcuts import render_to_response
    def foo(request):
        data_dic = {}
        # Populate the data dictionary here.
        # ...
        return render_to_response('foo.html', data_dic)    
  10. Link the view function with a URL pattern. Open and edit the file by adding the highlighted line:

    from django.conf.urls.defaults import *
    # Uncomment the next two lines to enable the admin:
    # from django.contrib import admin
    # admin.autodiscover()
    urlpatterns = patterns('',
        # Example:
        # (r'^sigcse/', include('')),    
        (r'^foo/', ''),
        # Uncomment the admin/doc line below to enable admin documentation:
        # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
        # Uncomment the next line to enable the admin:
        # (r'^admin/', include(,
  11. Using your web browser, visit the following URL: http://localhost:8000/foo/

How Everything Fits Together

The following figure illustrates how the view, the model and the template components fit together in Django in the context of a complete HTTP request/response cycle.

© 2009-2011 by Ariel Ortiz. Except where otherwise noted, content on this site is licensed under a
Creative Commons Attribution-Noncommercial 3.0 License