Django: Getting Started Transcripts
Chapter: HTML Templates
Lecture: Rendering the template

Login or purchase this course to watch this video and the rest of the course contents.
0:00 This is the stub file created by Django when I ran the start app command, let me just put in a comment header and now I'm ready to write my view.
0:14 You'll recall from the previous chapter that a view function returns an http response object
0:19 A really common pattern is to load and render a template and return it wrapped in an http response object.
0:26 So Django provides a shortcut for doing all that called render. This is so common that the stub provides that import for you out of the box.
0:36 Let me define a view. I'm going to call it sample, and like all views, it takes a request object as its first argument.
0:47 Now I'll create a dictionary that is used as a context for template rendering.
0:57 Everything I put in this dictionary is treated as a variable inside of the template.
1:02 You're not exposing everything in your Python function to the rendering engine. Just what is in the context.
1:08 The only thing I'm putting in the dictionary is a list of books. With each item in the list, also being a dictionary representing the book.
1:17 If you think back to the template I just wrote, you'll probably be able to take a good guess as to what the output will be
1:23 The last item in the view is the render shortcut. Render returns an http response object. And since views are supposed to return one of those,
1:36 you usually just return the result of render. Renders first argument is the request object for their view.
1:45 And the second argument is the name of a template file. The last argument is the rendering context, in this case the data dictionary.


Talk Python's Mastodon Michael Kennedy's Mastodon