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.
|
| |
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.
Listing 5.2 shows the complete views.py file.
Listing 5.2. Full Contents of the iFriends\People\views.py File
Code View: 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
|
|