Function Views
What this is. A view is a Python function. It receives a request object and must return a response.
That is the entire contract. Everything Django offers on top of it is convenience.
render() does three things. It loads a template, fills it with the context you pass, and wraps the result in an HttpResponse.
The third argument, the context, is a dictionary. Its keys are exactly the names the template can use, and nothing else is visible to the template. When a value is mysteriously blank on the page, this dictionary is the first place to look.
get_object_or_404. Fetching one object by id is so common that Django gives it a shortcut which raises a proper 404 when nothing matches, instead of an exception page.
Use it in views. Use plain .get() only where you have already established the object exists.
✦ Every path must return
"The view didn't return an HttpResponse object" means a branch fell off the end. It is nearly always an if with no else, on the path you did not test. Read the function and check that every route through it ends in a return.
Mark this lesson complete
Signed in, your progress follows you to every device.