zoukankan      html  css  js  c++  java
  • Django基础,Day10

    作为一个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 emplatespollsindex.html 中即可访问。

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

    mysite emplatespollsindex.html

    mysitepolls emplatespollsindex.html

    则会访问 mysite emplatespollsindex.html 而不是 mysitepolls emplatespollsindex.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测试开发圈”,了解更多测试教程!***
  • 相关阅读:
    Web系统可复用架构
    asp.net mvc 2 简简单单做开发 实现基本数据操作操作RepositoryController<T>
    asp.net mvc 2 简简单单做开发 使用DataContext扩展方法Find<TEntity>(TEntity obj) 遇到的问题
    asp.net mvc 2 简简单单做开发 自定义DropdownList控件
    asp.net mvc 2 DisplayTemplates 的使用
    linq to sql 扩展方法
    asp.net mvc 2 简简单单做开发 通用增删改基本操作通用页面
    asp.net mvc 2 简简单单做开发 自定义Controller基类
    JNLP启动相关的东东
    easyexcel往已存在的excel文件里追加数据
  • 原文地址:https://www.cnblogs.com/guanfuchang/p/6260459.html
Copyright © 2011-2022 走看看