回顾:

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 instantclient + plsql 远程连接数据库

    PLSQL Developer连接数据库:   1.不连接数据库登陆PLSQL Developer(登陆界面按“取消”即可).在Tools->Perferences->Connection ...

  2. [原]vue中各模块的实际引用

    检查发现: 1.vue实际引用文件配置位置 alias: { 'vue$': 'vue/dist/vue.esm.js', 此位置替换了vue包内的package.json中定义的位置 } 2.vue ...

  3. [mybatis]Example的用法

    Example类是什么? Example类指定如何构建一个动态的where子句. 表中的每个non-BLOB列可以被包括在where子句中. 例子是展示此类用法的最好方式. Example类可以用来生 ...

  4. maven学习-基本入门用法

    一.下载及安装 1.1 下载maven 3.1.1 先到官网http://maven.apache.org/download.cgi 下载最新版本(目前是3.1.1 ),下载完成后,解压到某个目录(本 ...

  5. Javascript面试题收集

    第一部分“ 来源: http://bbs.miaov.com/forum.php?mod=viewthread&tid=6974 1.var a = b = 1; ——这样定义变量的隐患 fu ...

  6. c#mvc实现登录

    本篇介绍MVC实现登录的方式,如下: 1.通过MVC Form 表单请求实现登录 2.通过AJAX GET 请求MVC Controller 实现登录 3.通过AJAX POST 请求MVC Cont ...

  7. 特大数字之和,返回结果是字符串(考虑到数字特别大,如果相加会产生e)

    自己做的,没有整理代码,还是做出来了: 做这个题时,最总要的一步思路就是,先让长度一致,然后从个位开始,每一个与每一个数字相加,如果大于10,则下一次另外两个数相加时加1 function add(a ...

  8. java引用

    java1.2之后将引用分为强引用(Strong Reference).软引用(Soft Reference).弱引用(Weak Reference).虚引用(Phantom Reference)4种 ...

  9. 528. Random Pick with Weight index的随机发生器

    [抄题]: Given an array w of positive integers, where w[i] describes the weight of index i, write a fun ...

  10. LWIP学习

    转自:https://blog.csdn.net/kzq_qmi/article/details/46900589 数据包pbuf:    LwIP采用数据结构 pbuf 来描述数据包,其结构如下:  ...