appendix E Sample exercise solutions
Throughout the book, there have been exercises to help you practice what you’ve learned. This appendix contains solutions to the exercises, in case you get stuck or want to compare your solution to my own. Coding is like writing; everyone has their own style, so if your code worked, it’s right, but it’s always useful to see how others approach a design as a learning exercise.
E.1 Chapter 2
Chapter 2 is all about getting started, building your first Django project and an app with a view to go inside it.
To build the About page, you need to create a new view function, which can be seen in the following listing.
Listing E.1 The view function for the About page
# RiffMates/home/views.py from django.http import HttpResponse ... def about(request): content = [ #1 "<!doctype html>", '<html lang="en">', "<head>", " <title>RiffMates About</title>", "</head>", "<body>", " <h1>RiffMates About</h1>", " <p>", " RiffMates is a for musicians seeking musicians. Find your next ", " band or band-mate. Find your next gig.", " </p>", "</body>", "</html>", ] content = "\n".join(content) #2 return HttpResponse(content) #3