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
and HttpResponse
objects manually-unless your employer judges your work by total lines of code written, that is.
Here's the ongoing current_datetime
example rewritten to use render()
:
from django.shortcuts import render import datetime &...