Django Views and URLconfs – Your First Django page

This mini tutorial explains how to serve a simple web page through Django that display’s a traditional ‘Hello world’ message.

The examples on this page assume that you have completed the Django installation, and have started a new Django project in a directory named /mysite/.

Key concepts

In Django, the contents of the page are produced by view a function, which you place in a file you create, conventionally named The URL of the page itself is specified in a URLconf,, which you specify in

Sample view

To output a simple Hello world! message, add this to your new

from django.http import HttpResponsedef hello(request):     return HttpResponse("Hello world")


  • Import the HttpResponse class, from django/http/ (or, the django.http module)
  • Define a function called hello. This is our view function.
  • Return an HttpResponse object that’s been instantiated with the text Hello world.

In order for a Python function to be a Django view, it must do two things:

  1. Takes an HttpRequest as its first parameter.
  2. Return an instance of HttpResponse.

Each view function takes at least one parameter, called request by convention.

Sample URLconf

Default URLconf:

If we ignore the code that’s commented out of the file (the one that was created when you first created your Django testProject folder), we’re left simply with:

from django.conf.urls.defaults import *urlpatterns = patterns('',)


  • Import all objects from the django/conf/urls/defaults/ (the django.conf.urls.defaults module). This includes the proceeding function, patterns.
  • Calls the function patterns and save the result into a variable called urlpatterns.

Edit the URLconf:

from django.conf.urls.defaults import *from mysite.views import hellourlpatterns = patterns('',    ('^hello/$', hello),)


  • Import the hello view from the module.
  • Any request to the URL /hello/ should be handled by the hello view function.

Things to remember

  • mysite/ must be on your Python path.
  • If you want to load this page from Apache as instead of as from Django’s development server, make sure that the following is in Apache’s configuration file (httpd.conf):

    <Directory C:/mysite>
    Order deny,allow
    Allow from all </Directory>
  • The URLpattern ('^hello/$', hello), is a Python tuple in which the first element is a regular expression, and the second is the view function to use for that pattern.

Test your first Django page

Either run the command python runserver to view your /hello/ page in Django’s development server at, or view it with your Apache web server at .

The page you see from this example is static; the content doesn’t change from user input, nor does it involve any database. Move on to a simple example of a dynamic Django page—as well as to an introduction to dynamic URLs—in Chapter 3 of DjangoBook: Views and URLconfs.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s