Now that we know how a view works and what it needs to do, let's write the real view to work with our sample application.
In your mycompany/press/views.py
file, replace any contents with the following lines:
from django.http import HttpResponse from django.http import HttpResponseNotFound from mycompany.press.models import PressRelease def detail(request, pid): ''' Accepts a press release ID and returns the detail page ''' try: p = PressRelease.objects.get(id=pid) return HttpResponse(p.title) except PressRelease.DoesNotExist: return HttpResponseNotFound('Press Release Not Found')
If you'd like to test it out, point your browser to http://localhost:8000/press/detail/1/
. You should see the title of your press release. Change the number at the end of the press release to an ID that doesn't exist (such as 99) and you should get a Page Not Found error.
This view doesn't return a very pretty output, but...