码迷,mamicode.com
首页 > 其他好文 > 详细

Django基础,Day10 - template 模板引擎与路径设置

时间:2017-01-07 22:47:06      阅读:539      评论:0      收藏:0      [点我收藏+]

标签:alt   内容   而不是   方法   支持   deb   模板   实例   log   

作为一个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.

 

 

Django基础,Day10 - template 模板引擎与路径设置

标签:alt   内容   而不是   方法   支持   deb   模板   实例   log   

原文地址:http://www.cnblogs.com/guanfuchang/p/6260459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!