Hello World!

  1. Create a new web project called sigcse. We will use this project for all other examples. Type at the command line:

    django-admin.py 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
    __init__.py Used to indicate that the current folder is a Python package.
    manage.py Utility script that allows you to interact with the current Django project in various ways.
    settings.py Configuration file for this Django project.
    urls.py The URL mappings for this Django project.
  2. Run the web server to see if it works correctly. Type at the command line:

    python manage.py runserver

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

  3. Create a templates folder. At the command line type:

    mkdir templates
  4. Tell Django the location of your templates folder. Edit the sigcse/settings.py file. Add a line similar to the one being highlighted:

    TEMPLATE_DIRS = (
        # 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.
        '/home/aortiz/sigcse/templates',
    )
  5. Create a base.html file and place it in the templates folder. The contents of this file should be:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
      <head>
    
        <style type="text/css">
    
          body {
            background: #FFFFFF;
            color: #000080;
            font-family: sans-serif;
            font-size: medium;
            margin: 20px;
          }
          
          hr {
            border: 0;
            color: #B0C4DE;
            background-color: #B0C4DE;
            height: 1px;
          }
    
          table {
            border-collapse: collapse;
            border: 1px solid #000000;
            padding: 0px;
            margin-bottom: 10px;
            color: #000000; 
            background: #E6E6FA;
          }
    
          td {
            border: 1px solid #000000;
            padding: 10px;
            text-align: center;
          }
    
          th {
            border: 1px solid #000000;
            padding: 10px;
            color: #008080;
            background: #B0E0E6;
          }
          
          .footer {
            font-size: 70%;
            color: #B0C4DE;
            text-align: center;
          }
    
        </style>
    
        <title>
          {% block title %}No Title{% endblock %}
        </title>
    
      </head>
    
      <body>    
        {% block main %}
    
        {% endblock %}
        <hr/>    
        <p class="footer">
          Workshop: <em>Web Development with Python and Django</em>
          <br/>
          <strong>SIGCSE 2009</strong>        
        </p>
      </body>
    
    </html>
  6. Create a new application called hello. At the command line, type:

    python manage.py startapp hello
  7. Create a hello.html file in the templates folder. The contents of this file should be:

    {% extends "base.html" %}
    
    {% block title %}Hello{% endblock %}
    
    {% block main %}
      <h1>Hello {{ name }}!</h1>
      <p>
        Yes! This is my first Django Web page.
      </p>
    {% endblock %}
    
  8. Create a view that sends information to the template. Edit the file sigcse/hello/views.py so that it has the following content:

    from django.shortcuts import render_to_response
    
    def say(request, name):
        return render_to_response('hello.html', {'name': name})    
    
  9. Link the view function with a URL pattern. Edit the sigcse/urls.py file by adding the highlighted line:

    urlpatterns = patterns('',
        # Example:
        # (r'^sigcse/', include('sigcse.foo.urls')),
    
        # Uncomment the admin/doc line below and add 'django.contrib.admindocs' 
        # to INSTALLED_APPS to enable admin documentation:
        # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    
        # Uncomment the next line to enable the admin:
        # (r'^admin/(.*)', admin.site.root),
        (r'^hello/(\w+)/$', 'sigcse.hello.views.say'),
    )
  10. Using your web browser, visit the following URL: http://localhost:8000/hello/World/
© 2009 by Ariel Ortiz. Except where otherwise noted, content on this site is licensed under a
Creative Commons Attribution-Noncommercial 3.0 License