作为一个Web框架,Django需要一个方便的方式来生成动态的HTML。最常见的方法依赖于模板。模板包含所需的HTML输出的静态部分以及一些特殊的语法描述如何插入动态内容。

Django框架后端默认支持自生内置的一套模板系统DTL(Django Template Language) 和 有名的Jinja2模板系统。当然,也可以从第三方模块中之前其他模板系统。如果没有特殊要求,建议使用Django自带的DTL模板系统,这也是django 1.8之前唯一可以的内置选项。

TEMPLATE 默认配置

settings.py:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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',
],
},
},
]

BACKEND:模板引擎类的python路径,内置的模板引擎分别有'django.template.backends.django.DjangoTemplates'和'django.template.backends.jinja2.Jinja2'

DIRS:模板引擎搜索模板的路径,如上,默认搜索project目录下的templates目录

APP_DIRS:告诉模板引擎是否搜索app目录下的templates目录。默认为true,即是默认搜索app目录下的templates目录

实例说明

根据前面章节的实例,可见我们使用了默认的TEMPLATE设置,html页面直接存放在app目录下的template目录下,如 polls/templates/polls/index.html.

在view.index 中直接使用

return render(request, 'polls/index.html', context)

即可渲染到polls/index.html 页面。

如果设置为 'APP_DIRS': False,则会搜索模板失败,如:

根据错误信息,可见模板引擎搜索模板的路径在project下的templates中。如果将index.html 移动到mysite\templates\polls\index.html 中即可访问。

若以上 'APP_DIRS': True,且同时存在

mysite\templates\polls\index.html

mysite\polls\templates\polls\index.html

则会访问 mysite\templates\polls\index.html 而不是 mysite\polls\templates\polls\index.html

多模板引擎设置

Django项目可以配置一个或多个模板引擎(甚至是零,如果你不使用模板)。

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'/home/html/example.com',
'/home/html/default',
],
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
'/home/html/jinja2',
],
},
]

If you call get_template('story_detail.html'), here are the files Django will look for, in order:

  • /home/html/example.com/story_detail.html ('django' engine)
  • /home/html/default/story_detail.html ('django' engine)
  • /home/html/jinja2/story_detail.html ('jinja2' engine)

If you call select_template(['story_253_detail.html', 'story_detail.html']), here’s what Django will look for:

  • /home/html/example.com/story_253_detail.html ('django' engine)
  • /home/html/default/story_253_detail.html ('django' engine)
  • /home/html/jinja2/story_253_detail.html ('jinja2' engine)
  • /home/html/example.com/story_detail.html ('django' engine)
  • /home/html/default/story_detail.html ('django' engine)
  • /home/html/jinja2/story_detail.html ('jinja2' engine)

When Django finds a template that exists, it stops looking.


***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***

Django基础,Day10 - template 模板引擎与路径设置的更多相关文章

  1. Django基础,Day9 - 静态文件目录与路径设置说明(eg. images, JavaScript, CSS)

    静态文件路径设置官方说明 1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS. 2. In ...

  2. day 68 Django基础四之模板系统

      Django基础四之模板系统   本节目录 一 语法 二 变量 三 过滤器 四 标签Tags 五 模板继承 六 组件 七 自定义标签和过滤器 八 静态文件相关 一 语法   模板渲染的官方文档 关 ...

  3. day 54 Django基础四之模板系统

    Django基础四之模板系统   本节目录 一 语法 二 变量 三 过滤器 四 标签Tags 五 模板继承 六 组件 七 自定义标签和过滤器 八 静态文件相关 一 语法   模板渲染的官方文档 关于模 ...

  4. 织梦CMS(dedecms)栏目属性及系统封面模板、列表模板、文章模板区别和路径设置解答

    问题一:(织梦"栏目管理"的"常规选项"中3个栏目属性分析?) 织梦CMS的栏目属性分成三种, -->最终列表栏目 -->频道封面 -->外部 ...

  5. django基础知识之模板:

    模板介绍 作为Web框架,Django提供了模板,可以很便利的动态生成HTML 模版系统致力于表达外观,而不是程序逻辑 模板的设计实现了业务逻辑(view)与显示内容(template)的分离,一个视 ...

  6. Django基础之template

    1. 模板系统的介绍 def current_datetime(request): now = datetime.datetime.now() html = "<html>< ...

  7. [JavaWeb基础] 020.Velocity 模板引擎简单示例

    1.什么是Velocity 一种J2EE的前端模版技术,和JSP,Freemarker差不多,都是用来展示网页内容的.和JSP不同的是velocity只能显示Action中的数据,不能处理数据.不能写 ...

  8. 04.Django基础四之模板系统

    一 语法 模板渲染的官方文档 关于模板渲染你只需要记两种特殊符号(语法): {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二 变量 在Django的模板语言中按此语法使用:{ ...

  9. Django基础四之模板系统

    一 语法   模板渲染的官方文档 关于模板渲染你只需要记两种特殊符号(语法): {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二 变量 在Django的模板语言中按此语法使 ...

随机推荐

  1. DTD中的属性类型

    <![CDATA[文本内容]]> DTD中的属性类型 全名:character data 在标记CDATA下,所有的标记.实体引用都被忽略,而被XML处理程序一视同仁地当做字符数据看待, ...

  2. UITableView的cell重用优化

    三种情况,四种方法: 情况一:加载xib中描述的cell 情况二:加载纯代码自定义的cell 情况三:加载storyBoard中的tableView内的cell 针对于情况一: // 导入自定义cel ...

  3. oninput等表单事件

    oninput等表单事件 过去我们常使用keydown和keyup辅助表单元素的处理,这要求处理时,表单元素必须处于激活(聚焦)状态.oninput事件可以实时监听文本框的输入变化.   现代浏览器支 ...

  4. Top K 问题

    Example Given [3,10,1000,-99,4,100] and k = 3. Return [1000, 100, 10]. 解法有以下几种: 1. bubble sort k tim ...

  5. Leetcode Move Zeros

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  6. 【matlab】设定C++编译器

    在用matlab2016a,运行的代码涉及到C++混编,要用mex把C++代码编译出来. 记得几年前的matlab版本,通过: mex -setup 就可以设定了,选择要使用的编译器(文本的超链接), ...

  7. phpMyadmin /scripts/setup.php Execute Arbitrary PHP Code Via unserialize Vul Object Injection PMASA-2010-4

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 对这个漏洞简单的概括如下 . "/scripts/setup.php&q ...

  8. ansible模块debug

    示例: # Example that prints the loopback address and gateway for each host - debug: msg="System { ...

  9. ansible模块lineinfile

    示列: sshd_set.yaml --- - hosts: test remote_user: root gather_facts: False tasks: - name: set hostnam ...

  10. Pythonn new-style class and old-style class

    In [1]: class old(): ...: a = 1 ...: In [2]: o = old() In [3]: o.__class__ Out[3]: <class __main_ ...