回顾:

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. 三. html&JavaScript&ajax 部 分

    1. 判 断 第 二 个 日 期 比 第 一 个 日 期 大 如何用脚本判断用户输入的的字符串是下面的时间格式2004-11-21必须要保证用户 的输入是此格式,并且是时间,比如说月份不大于12等等, ...

  2. leetcode76

    class Solution: def minWindow(self, s: str, t: str) -> str: n = len(s) if n==0: return "&quo ...

  3. leetcode406

    public class Solution { public int[,] ReconstructQueue(int[,] people) { ) { return new int[,] { }; } ...

  4. EasyARM-iMX283A的make menuconfig出现错误:Install ncurses(ncurses-devel) and try again。

    lin@lin-machine:~/linux-2.6.35.3$ make menuconfig *** Unable to find the ncurses libraries or the ** ...

  5. C#对接JAVA系统遇到的AES加密坑

    起因对接合作伙伴的系统,需要对数据进行AES加密 默认的使用了已经写好的帮助类中加密算法,发现结果不对,各种尝试改变加密模式改变向量等等折腾快一下午.最后网上查了下AES在JAVA里面的实现完整代码如 ...

  6. mysql 乐观锁实现

    一.为什么需要锁(并发控制)?      在多用户环境中,在同一时间可能会有多个用户更新相同的记录,这会产生冲突.这就是著名的并发性问题.      典型的冲突有:        1.丢失更新:一个事 ...

  7. 关于echarts图表在tab页中width:100%失效的问题

    https://www.cnblogs.com/tongrenlu/p/9268250.html

  8. ztree带有选项框的树形菜单使用

    1.ztree简介 zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点.专门适合项目开发,尤其是 树状菜单.树状数据的Web ...

  9. linux下redis4.0.2集群部署(利用原生命令)

    一.部署架构如下 每台服务器准备2个节点,一主一从,主节点为另外两台其中一台的主,从节点为另外两台其中一台的从. 二.准备6个节点配置文件 在172.28.18.75上操作 cd /etc/redis ...

  10. 小强学渲染之OpenGL状态机理解

    状态机是理论上的一种机器,呃这个说法非常非常的抽象.通俗一点理解,状态机描述了一个对象在其生命周期内所经历的各种状态,状态间的转变,发生转变的动因,条件及转变中所执行的活动.或者说,状态机是一种行为, ...