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',],
        },
    },]

最终页面效果如下:

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM