成都创新互联公司凭借在网站建设、网站推广领域领先的技术能力和多年的行业经验,为客户提供超值的营销型网站建设服务,我们始终认为:好的营销型网站就是好的业务员。我们已成功为企业单位、个人等客户提供了成都做网站、成都网站建设服务,以良好的商业信誉,完善的服务及深厚的技术力量处于同行领先地位。
目录
template:...1
设置template引擎:...1
设置公用templates和公用static:...2
view中使用template:...4
去掉url和static硬编码:...5
获取和渲染模板:...5
内置built-in template tags和filters:...7
自定义template filter:...7
模板扩展extends和include包含:...8
django内置了自己的模板引擎,和jinja很像,使用简单;
django默认会在app_name/templates/下寻找模板,在app_name/templates/下再建立app_name,这样app_name/templates/app_name/下存放与该app相关的模板,因为默认django会去所有的app下找模板,可能会优先找到其它app下的模板;
默认会到app_name/static/下寻找静态文件;
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
context_processors are functions that receive the current HttpRequest as an argument and return a dict of data to be added to the rendering context.
mysite/mysite/settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
……
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
variables:
{{ var }}、{{ dict.key }}、{{ var.attr }}、{{ var.method }}、{{ var.index }}
模板引擎支持循环、判断、过滤器:
filter:
{{ list|join:"," }}、{{ name|lower }}
{{ now | date:"Y-m-d" }}
{{ name | length }}
tags:
{% tag xxx %}{% endtag %}
{% for ... %}{% endfor %}
{# comment #}
for:
{% for person in person_list %}
{% endfor %}
if:
{% if max > 10 %}
{% else %}
{% endif %}
例:
>>> from django.template import Template,Context
>>> t = Template('My name is {{ name }}')
>>> c = Context({'name':'jowin'}) #context可以是 dict、属性、方法、tuple|list
>>> t.render(c)
'My name is jowin'
>>> t = Template('my name is {{ user.name }}') #变量查找,dict、attr、method、list、tuple
>>> class Person:
... def __init__(self,name):
... self.name = name
...
>>> user = Person('jowin')
>>> user.name
'jowin'
>>> c = Context({'user':user})
>>> t.render(c)
'my name is jowin'
>>> t = Template('my name is {{ user.name }}')
>>> class Person:
... def name(self):
... return 'jowin'
...
>>> user = Person()
>>> user.name()
'jowin'
>>> c = Context({'user':user})
>>> t.render(c)
'my name is jowin'
render源码,from django.shortcuts import render:
def render(request, template_name, context=None, content_type=None, status=None, using=None):
"""
Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
content = loader.render_to_string(template_name, context, request, using=using)
return HttpResponse(content, content_type, status)
另,render_to_response(),老版本1.6用;
mysite/polls/views.py
from django.http import HttpResponse
from .models import Question
from django.template import loader
# def index(request):
# latest_question_list = Question.objects.order_by('-pub_date')[:4]
# template = loader.get_template('polls/index.html')
# context = {'latest_question_list': latest_question_list}
# # output = ', '.join([q.question_text for q in latest_question_list])
# return HttpResponse(template.render(context)) #方1
def index(request):
latest_question_list = Question.objects.order_by('-pub_date')[:4]
context = {'latest_question_list': latest_question_list}
return render(request, 'polls/index.html', context) #方2
mysite/polls/templates/polls/index.html
#mysite/polls/static/django.png
{% if latest_question_list %}
{% for question in latest_question_list %}
{% endfor %}
{% endif %}
mysite/polls/templates/polls/index.html
{% load static %}
{% if latest_question_list %}
{% for question in latest_question_list %}
{% endfor %}
{% endif %}
注:
{% url 'detail' question.id %} #app的urls.py中定义的name有关;app namespace改后,'detail'改为'polls:detail';
方1:
from django.template.loader import get_template
def get_template(template_name, using=None): #返回一个模板对象,再使用模板对象的类方法Template中的render()渲染
"""
Loads and returns a template for the given name.
Raises TemplateDoesNotExist if no such template exists.
"""
chain = []
engines = _engine_list(using)
for engine in engines:
try:
return engine.get_template(template_name)
except TemplateDoesNotExist as e:
chain.append(e)
raise TemplateDoesNotExist(template_name, chain=chain)
class Template(object):
def __init__(self, template_string, origin=None, name=None, engine=None):
……
def _render(self, context):
return self.nodelist.render(context)
def render(self, context):
"Display stage -- can be called many times"
with context.render_context.push_state(self):
if context.template is None:
with context.bind_template(self):
context.template_name = self.name
return self._render(context)
else:
return self._render(context)
方2:
from django.shortcuts import render
def render(request, template_name, context=None, content_type=None, status=None, using=None):
"""
Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
content = loader.render_to_string(template_name, context, request, using=using)
return HttpResponse(content, content_type, status)
方3:
from django.template.loader import render_to_string
def render_to_string(template_name, context=None, request=None, using=None):
"""
Loads a template and renders it with a context. Returns a string.
template_name may be a string or a list of strings.
"""
if isinstance(template_name, (list, tuple)):
template = select_template(template_name, using=using)
else:
template = get_template(template_name, using=using)
return template.render(context, request)
block #Defines a block that can be overridden by child templates.,{% block custom_title %}首页{% endblock %}
csrf_token #html的form标签中使用,{% csrf_tokne %}
extends #模板扩展,{% extends 'base.html' %}
include #模板包含,{% include 'pagination.html' %}
url #
filter
for
for ... empty
if
例:
{% for ad in ads %}
{% endfor %}
过滤器使用格式:
{{ 变量|过滤器:参数 }}
add #
center
date #
default
dictsort
dictsortreversed
filesizeformat
length
last
ljust
lower
random
rjust
safe # 告诉browser,这个变量是安全的,可以渲染,没必要显示纯文本,{{ article.content | safe }}
title
truncatewords #如果后端传的数据太多,只显示一部分,{{ value|truncatewords:3 }},截取3个单词
i18n
tz
static #{% load static %}吗{% load staticfiles %}
get_static_prefix #
get_media_prefix
例:
{% extends "base.html" %}
{% block content %}
{% for publisher in object_list %}
{% endfor %}
{% endblock %}
{% ifequal a b %} ... {% endifequal %} is an obsolete way to write {% if a == b %} ... {% endif %}
Likewise,
{% ifnotequal a b %} ... {% endifnotequal %} is superseded by {% if a != b %} ... {% endif %}
The ifequal and ifnotequal tags will be deprecated in a future release.
{% for current_org in hot_orgs %}
{{ current_org.address }}
{% endfor%}
django寻找自定义filter的目录是app_name/templatetags/;
例:
mysite/polls/templatetags/mytags.py
from django import template
register = template.Library()
@register.filter
def lower(text):
return text.lower()
@register.filter
def question_choice_count(question):
return question.choice_set.count()
@register.filter
def question_choice_count_add(question, num):
return question.choice_set.count() + int(num)
mysite/polls/templates/polls/index.html
{% load static %}
{% load mytags %}
{% if latest_question_list %}
{% for question in latest_question_list %}
-- {{ question|question_choice_count }}
{% endfor %}
{% endif %}
实现网页布局;
extends是继承模板,然后自定义可设置的block;
include是导入一个模板片段到该位置;