Hello World!

  1. Create a new web project called sigcse. We will use this project for all other examples. 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. Create a templates folder. At the command line type:

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

        # 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.
  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"
    <html xmlns="">
        <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;
          {% block title %}No Title{% endblock %}
        {% block main %}
        {% endblock %}
        <p class="footer">
          Workshop: <em>Web Development with Python and Django</em>
          <strong>SIGCSE 2009</strong>        
  6. Create a new application called hello. At the command line, type:

    python 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>
        Yes! This is my first Django Web page.
    {% endblock %}
  8. Create a view that sends information to the template. Edit the file sigcse/hello/ 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/ file by adding the highlighted line:

    urlpatterns = patterns('',
        # Example:
        # (r'^sigcse/', include('')),
        # 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/(.*)',,
        (r'^hello/(w+)/$', 'sigcse.hello.views.say'),
  10. Using your web browser, visit the following URL: http://localhost:8000/hello/World/


版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.



你的邮箱是保密的 必填的信息用*表示