zoukankan      html  css  js  c++  java
  • django母版页的使用

    母版页用于处理html页面相同部分内容,避免在不同的页面中重复出现

    1、添加母版页

      再manage.py文件相同目录下添加templates文件夹用于保存母版页html文件

    2、添加母版页Base.html,html如下:

    <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <h1>这是模板页</h1>
        {% block content%}
    
        {% endblock %}
    </body>
    </html>

    使用 block进行占位 

    3、在具体页面使用母版页

    {% extends 'Base.html' %}
    {% block content%}
    This is ChildPageContent
    {% endblock %}

    4、由于母版页和子页面不在同一个APP下,需要在主模块的settings.py 文件里面进行路径配置,在同一app下则不需要配置,

      在TEMPLATES 下的DIRS里面配置母版页所在路径

      具体配置如下:

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

    最终页面效果如下:

      

  • 相关阅读:
    hd CPU
    OS + Linux Interview
    db db2 v8.2
    project ERP_SAP R3 / SAP ECC / mySap
    命名空间和模块化编程3 C++快速入门41
    链接和作用域 C++快速入门42
    对象观察器(Object Inspector)
    Delphi7的窗体设计器
    对象观察器(Object Inspector)
    链接和作用域 C++快速入门42
  • 原文地址:https://www.cnblogs.com/tangchun/p/8487445.html
Copyright © 2011-2022 走看看