Previous Page Next Page

Adding Data in a Model to a View

So far in this hour, you have used the Django shell to access the database. This section switches to creating web pages. Ultimately, most of the database queries that you will perform on the database will be done from the view functions in the views.py file and will be used to generate web pages.

Using the database-abstraction API that we have covered so far in this hour, you can retrieve data from the database in your views and then use that data to build web pages that contain meaningful content. Adding data from the database to a web page is simple. You add the Python code in the view necessary to retrieve the data from the database and then use that data when constructing the web page.

Try It Yourself: Display Data from the Database in Views

In the past hours, you have had a chance to generate some web pages using HttpResponse objects in views. However, the web pages that were generated had simple headings as the content.

In this section, you will use the database-abstraction API in the views to add content from the database to web pages in the iFriends project.

Follow these steps to modify the index() and details() view so that the index() view displays a list of Person objects in the database and the details() view displays detailed information about a specific version:

1.
Open the iFriends/People/views.py file in an editor.

2.
Add the following code to the index() function, shown in Listing 5.2, to create an HttpResponse object and begin generating the HTTP response:

response = HttpResponse()
response.write("<html><body>\n")
response.write("<h1>People</h1><hr>")

3.
Use the all() function to retrieve all Person objects from the database:

pList = Person.objects.all()

4.
Add the following code snippet to iterate through the QuerySet that was generated in step 3, and generate a list of links for each Person object:

for p in pList:
    link = "<a href=\"Info/%d\">" % (p.id)
    response.write("<li>%s%s</a></li>" % (link, p.name))

5.
Add the following code snippet to finish and return the HTTP response:

response.write("</body></html>")
return response

6.
Verify that the URL pattern works correctly by accessing the URL http://127.0.0.1:8000/People. You should see a web page similar to the one shown in Figure 5.7.

Figure 5.7. index() view for the People application.


7.
Add the following code to the details() function, shown in Listing 5.2, to query the database and find the Person object with an id field that matches the pID parameter passed into the function and handle cases where no object is found:

try:
    p = Person.objects.get(id=pID)
    ...
except Person.DoesNotExist:
    response.write("Person Not Found")

8.
Add the following code snippet to display detailed information about the Person object:

response.write("<h1>Details for Person %s</h1><hr>\n" % p.name)
response.write("<li>Birthday: %s</li>" % p.birthday)
response.write("<li>Email: %s</li>" % p.email)
response.write("<li>Favorite URL: %s</li>" % p.favoriteURL)
response.write("<li>Desc: %s</li>" % p.desc)

9.
Add the following code snippet to finish and return the HTTP response:

response.write("</body></html>")
return response

10.
Verify that the URL pattern works correctly by clicking one of the links on the details view, as shown in Figure 5.7. You should see a web page similar to the one shown in Figure 5.8.

Figure 5.8. Details for a Person object.


Listing 5.2 shows the complete views.py file.

Listing 5.2. Full Contents of the iFriends\People\views.py File

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

def index(request):
    response = HttpResponse()

    response.write("<html><body>\n")
    response.write("<h1>People</h1><hr>")
    pList = Person.objects.all()
    for p in pList:
        link = "<a href=\"Info/%d\">" % (p.id)
        response.write("<li>%s%s</a></li>" % (link, p.name))
    response.write("</body></html>")

    return response

def details(request, pID='0'):
    response = HttpResponse()

    response.write("<html><body>\n")
    try:
        p = Person.objects.get(id=pID)
        response.write("<h1>Details for Person %s</h1><hr>\n" % p.name)
        response.write("<li>Birthday: %s</li>" % p.birthday)
        response.write("<li>Email: %s</li>" % p.email)
        response.write("<li>Favorite URL: %s</li>" % p.favoriteURL)
        response.write("<li>Desc: %s</li>" % p.desc)
    except Person.DoesNotExist:
        response.write("Person Not Found")
    response.write("</body></html>")

    return response


					  


Previous Page Next Page