A, get_template function
from django.template.loader import get_template
from django.http import HttpResponse
import datetime
def temp_test(request):
now = datetime.datetime.now()
t = get_template('temp_test.html')
html = t.render({'current_date': now})
return HttpResponse(html)
Template output:
It is now {{ current_date }}
get_template () function to the template name as a parameter to identify the location in the file system module, open the file and returns a compiled Template object
Two, render_to_response () renders the template
from django.shortcuts import render_to_response
import datetime
def current_datetime(request):
now = datetime.datetime.now()
return render_to_response('current_datetime.html', {'current_date': now})
render_to_response () first argument must be the name of the template you want to use. If you give the second parameter set, then it must be the time for template creation Context dictionary used. If no second argument, render_to_response () with an empty dictionary.
Three, locals () tips
from django.shortcuts import render_to_response
import datetime
def temp_test(request):
now = datetime.datetime.now()
return render_to_response('temp_test.html', locals())
Value about locals (), which encompasses all the variables to perform the function defined by the point in time
At this time, the output of the corresponding html format should be:
It is now {{ now }}
Four, get_template use subdirectories () in
from django.shortcuts import render_to_response
import datetime Zhengzhou gynecological hospital http://www.sptdfk.com/
def temp_test(request):
now = datetime.datetime.now()
return render_to_response('member/temp_test.html', locals())
Just before when calling get_template (), add the subdirectory name and a slash to the template name
Since the render_to_response () simply encapsulate to get_template (), you can the render_to_response first parameter () to do the same process
Fifth, the template contains templates and inheritance
1, the template contains: include (); public head, the tail of the public, subject to the current template directory path
{% include 'common/nav.html' %}
It is now {{ now }}
2, template inheritance: block usage
Concept: template inheritance is to first construct a basic framework template, then it contains the site of the common parts of the block and defined in its child template overloaded
My helpful timestamp site
{% block content %}{% endblock %}
{% block footer %}
Thanks for visiting my site.
{% endblock %}
All {% block%} tag tells the template engine, template may override those portions of the child. Each {% block%} tags do is tell the template engine, this piece of content in the template will be possible to cover the quilt template.
The sub-template can be changed to:
{% extends "base.html" %}
{% block title %}The current time{% endblock %}
{% block content %}
It is now {{ current_date }}.
{% endblock %}
The core function point:
1, if the {% extends%} in a template, the template must first ensure that the template is labeled. Otherwise, template inheritance will not work.
2, generally, the foundation template {% block%} tag better. Remember, the sub-template is not necessary to define all the parent blocks of the template, and therefore may be filled with blocks of code in a reasonable default values, and then only the desired sub-block of code templates (re) defined
3. If you need to access the contents of the block from the parent template, use {{block.super}} this tag, it will show a magic variable contents of the parent template. If you want to add content to the parent block instead of completely overriding the variable it is very useful.
4, allowed the definition of {% block%} of the same name in the same template.
5, {% extends%} of the loading method using the template name and passed to get_template () the same. That is, the template name is added to the set after TEMPLATE_DIRS.
6, in most cases, {% extends%} of the parameter should be a string, but can only determine if the name of the parent template until runtime, this may be a variable parameter