render()
So far, we've shown you how to load a template, fill a Context
and return an HttpResponse
object with the result of the rendered template. Next step was to optimize it to use get_template()
instead of hard-coding templates and template paths. I took you through this process to ensure you understood how Django templates are loaded and rendered to your browser.
In practice, Django provides a much easier way to do this. Django's developers recognized that because this is such a common idiom, Django needed a shortcut that could do all this in one line of code. This shortcut is a function called render()
, which lives in the module django.shortcuts
.
Most of the time, you'll be using render()
rather than loading templates and creating Context ...
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.