Django: Getting Started Transcripts
Chapter: Course conclusion
Lecture: View functions
Login or purchase this course to watch this video and the rest of the course contents.
0:00 A view is a function or class responsible for returning HTML and is typically mapped to a URL.
0:07 Here, you see the book view which is implemented as a function. It takes two arguments, the request, and a book ID.
0:16 All function views take at least one argument the request. The request contains information about the http request that resulted
0:22 in the view being called. If your view takes other arguments, the URL mapping mechanism can be used to map parts of the URL
0:30 to the argument in the function. As well as doing typecasting so that your numbers can be actual Python numbers instead of
0:37 a string containing a number. There are a number of shortcut utility functions you can use to reduce the amount of code needed to write a view.
0:45 The get object or 404 shortcut does a look up of a model object. It takes at least two arguments.
0:52 The first is the name of the model being looked up and the second is any arguments in the filter that determines the object.
0:59 If the query results in a single object it is returned. Otherwise, this shortcut raises a 404 exception telling the user they tried to do something
1:08 they shouldn't have. Most views use templates to generate the HTML that is to be sent back to the browser. As this is such a common pattern
1:16 the render shortcut takes the name of a template and some context data and renders a result,
1:21 wrapping it in the needed http response object to be returned by the view.