Previous Page Next Page

Creating a Simple View

After you have configured the URLConf file, you need to add the views to the application. The application's views are stored as functions in the views.py file in the application directory. When the Django server receives an URL request, it parses the request based on the patterns that are contained in the URLConf file and determines which function to execute to generate the web view.

Try It Yourself: Create the Index View for the People Application

This section guides you through the steps of creating an index view stub for the People application in the iFriends project. After the view is created, you start the development server and view the web page that is generated.

1.
Open the iFriends/People/views.py file in an editor. The views.py file is empty at first.

2.
Use the editor to add the following code snippet to the file:

from django.shortcuts import HttpResponse
from iFriends.People.models import Person

def index(request):
    html = "<H1>People</H1><HR>"
    return HttpResponse(html)

3.
Save the file.

4.
From a command prompt, change to the root directory for the iFriends project.

5.
Enter the following command to start the development server:

python manage.py runserver

6.
Access the http://127.0.0.1:8000/People URL. You should see a web page similar to the one shown in Figure 2.6.

Figure 2.6. Accessing a custom index view in the Django project from a web browser.



Previous Page Next Page