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

最终页面效果如下:

  

转载于:https://www.cnblogs.com/tangchun/p/8487445.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值