Understanding the ViewsΒΆ
View:
def template3d_name(request, page=None):
if settings.DEBUG:
t = select_template([template3d().parse_template(page+'.html')])
else:
t = select_template([page+'.html'])
c = RequestContext(request, {})
return HttpResponse(t.render(c))
The select_template()
function returns rendered template data to the view. It will also parse the templates
directory for a given template name. This is why it is only necessary to provide the name of the template.
The template3d().parse_template()
command will parse the standard templates directory, (defined as STANDARD_TEMPLATE_DIRS), for the provided template name. It returns this name string back to any parent functions, e.g. select_template()
. Also, template optimizations will occur on the template and be placed into the django templates directory (defined as TEMPLATE_DIRS).
This is how the real time optimization works for development.
We return an HttpResponse
with the rendered data and context data (if any). It is not necessary to use Django shortcuts here such as render
. You may use the render command however by changing the view structure like this.
Shortcut View:
def template3d_name(request, page=None):
if settings.DEBUG:
return render(request, template3d().parse_template(page+'.html'), {} )
else:
return render(request, page+'.html' , {} )
Note
Do not use the render
shortcut with select_template()
. This will cause the template to be rendered twice, thus lowering
efficiency.