回顾:

Variables

{{ var }} {{ dict.key }} {{ var.attr }} {{ var.method }} {{ varindex }}

Filter

{{ list | join."," }}  {{ name | lower }}

Tags

{% tag xxx % } xxx {% endtag %}  {% for ... %} xxx {% endfor %}

{# comment #}

配置Template引擎

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',
],
},
},
]

获取和渲染模板

  • django.template.loader.get_template(template_name,using=None)

  • django.shortcuts.render()

  • Template.render(content=None,request=None)

  • django.template.loader.render_to_string(template_name, context=None, request=None, using=None)

其中 'APP_DIRS': True 可以向app目录下寻找模板

Context processors

Context processors 可以向HttpRequest中注入一些全局性参数

  • django.contrib.auth.context_processors.auth

    user

    perms

  • django.template.context_processors.debug

    debug

    sql_query

  • django.template.context_processors.media

    MEDIA_URL

自定义processors

1. 项目目录下新增 context_processors.py

自定义函数,第一个参数必须为 HttpRequest object,返回一个字典

def global_setting(request):
user = {
'name': 'alex',
'age': 18,
'sex': 'male'
}
return user

2. OPTIONS 添加路径

3. 前端展示

<h2>自定义context_processors</h2>
{{ name }}<br/>
{{ age }}<br/>
{{ sex }}<br/>

内置Template Tag 和 Filters

https://docs.djangoproject.com/en/1.11/ref/templates/builtins/

自定义Template Filter

Django寻找自定义filter的目录是 app_name/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)

前端使用,重启服务,加载标签

{% load static %}
{% load mytags %} <body>
<img src="{% static 'django.png' %}">
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:vote' question.id %}">{{ question.question_text }}</a>
-- {{ question | question_choice_count }} -- {{ question| question_choice_count_add:2 }}
</li>
{% endfor %}
</ul>
{% endif %}
</body>

模板扩展和包含

扩展 extends,是在继承模板,然后自定义可以设置block

包含 include,是导入一个模板片段到该位置

# mysite/templates/base.html
<html>
<head>
<title> {% block title %} {% endblock %}</title>
{% include '_head_css_js.html' %}
</head>
<body>
{% include '_header.html' %}
{% block content %}
{% endblock %}
{% include '_footer.html' %}
</body>
</html> # mysite/templates/_header.html
<div>
This is header
</div>
# mysite/templates/_footer.html
<div>
This is footer
</div>
# mysite/templates/_head_css_js.html
# mysite/templates/index.html
{% extends 'base.html' %}
{% block content %}
<h1> Index 1 </h1>
{% endblock %}

Django Template 进阶的更多相关文章

  1. Django【进阶篇 】

    Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行 ...

  2. Django web 进阶

    .路由系统 .模板引擎 simple_tag .Form .Ajax请求 -简单数据 -复杂数据 内容: -作业 model xss.csrf(安全方面的内容) 分页(公共的模块) 内容复习和今日内容 ...

  3. Python之路【第十七篇】:Django【进阶篇 】

    Python之路[第十七篇]:Django[进阶篇 ]   Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...

  4. Python之Django【进阶篇 】

    Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行 ...

  5. Python之路【第十七篇】:Django【进阶篇】

    Python之路[第十七篇]:Django[进阶篇 ]   Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...

  6. Python开发【第二十二篇】:Web框架之Django【进阶】

    Python开发[第二十二篇]:Web框架之Django[进阶]   猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 ...

  7. Python之路【第十七篇】:Django【进阶篇 】(转自银角大王博客)

    Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行 ...

  8. django template

    一.模板基本元素 1.例子程序 1)urls.py中新增部分 from django.conf.urls import patterns, url, include urlpatterns = pat ...

  9. Django.template框架 template context (非常详细)

    前面的章节我们看到如何在视图中返回HTML,但是HTML是硬编码在Python代码中的 这会导致几个问题: 1,显然,任何页面的改动会牵扯到Python代码的改动 网站的设计改动会比Python代码改 ...

随机推荐

  1. Oracle表中的主键被当成哪些表的外键

    SELECT B.TABLE_NAME FROM USER_CONSTRAINTS A INNER JOIN USER_CONS_COLUMNS B ON A.CONSTRAINT_NAME = B. ...

  2. python——字符串问题总结

    转义符r/R使用: print (r'\n') print (R'\n') 输出: \n \n 不受转义符\n影响 python字符串格式化: print ("我叫 %s 今年 %d 岁!& ...

  3. leetcode560

    public class Solution { public int SubarraySum(int[] nums, int k) { , result = ; Dictionary<int, ...

  4. Python __slots__限制动态添加变量

    Python是一种非常灵活的动态语言,有时感觉太灵活以至于不知道遵循什么样的规则去驾驭.不过Python已经是非常完备的语言,想实现什么样的功能都是有方法的,而且也很容易,比如限制一个类动态添加成员变 ...

  5. 保存xml报错 'UTF_8' is not a supported encoding name

    ArgumentException: 'UTF_8' is not a supported encoding name. For information on defining a custom en ...

  6. ArcGIS自定义工具箱-显示地图文档结构

    ArcGIS自定义工具箱-显示地图文档结构 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:显示地图文档mxd的数据组织结构,数据框,图层,表 使用方法: 地图 ...

  7. html背景图星际导航图练习

    html <body>         <div class="box1">            <div></div>      ...

  8. Sqlite安装教程

    Sqlite下载页面:http://www.sqlite.org/download.html Windows安装 需要下载 sqlite-tools-win32-*.zip 和 sqlite-dll- ...

  9. jQuery之必会增删改查Dom操作

    .next  .prev <button>change</button> <span class = '.demo'>aaa</span> <p ...

  10. 4、订单详情 /items/order/detail?orderNo=201903251750380001

    <template> <div class="write"> <div class="adr"> <div class ...