其他分享
首页 > 其他分享> > Django抽取前端公共页面

Django抽取前端公共页面

作者:互联网

在开发过程中,导航条、侧边栏等内容一般将被重复利用,这时候就需要抽取页面模板,减少代码书写。

废话不多说,上DEMO

 

1、公共页面,我一般习惯命名为 common.html,直接看标红地方

{% load static %}  载入公共页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <link href="../static/css/bootstrap.min.css" rel="stylesheet"> <script src="../static/js/jquery.min.js"></script> <script src="../static/js/bootstrap.js"></script> <link rel="stylesheet" href="../static/layui/css/layui.css"> <script src="../static/layui/layui.js"></script> <style> .step1 { margin-left: 30px; margin-right: 30px; height: 500px; clear: both; } </style> </head> <body> <nav class="navbar navbar-default navbar-dark bg-dark"> <div class="container-fluid"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="/">家客运维指挥作战平台</a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <!-- <li class="active"><a href="#"> <span class="sr-only">(current)</span></a></li>--> <!-- <li><a href="#">Link</a></li>--> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">快捷导航 <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="/tar_by_date">运维指标</a></li> <li><a href="zwhx.html">装维画像</a></li> <li><a href="#">投诉分析</a></li> <li role="separator" class="divider"></li> <li><a href="/proclamation">公告栏</a></li> <li role="separator" class="divider"></li> <li><a href="#">通讯录</a></li> </ul> </li> </ul> <ul class="nav navbar-nav"> <!-- <li class="active"><a href="#"> <span class="sr-only">(current)</span></a></li>--> <!-- <li><a href="#">Link</a></li>--> <li class="dropdown"> <a href="#" class="dropdown-toggle " data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">后台管理 <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="/insert">公告发布</a></li> <li><a href="/target">指标发布</a></li> </ul> </li> </ul> <form class="navbar-form navbar-left"> <div class="form-group"> <input type="text" class="form-control" placeholder="搜索"> </div> <button type="submit" class="btn btn-default">搜索</button> </form> <ul class="nav navbar-nav navbar-right"> <!-- <li><a href="#">Link</a></li>--> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> 用户 <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">登录</a></li> <li role="separator" class="divider"></li> <li><a href="#">退出</a></li> </ul> </li> </ul> </div><!-- /.navbar-collapse --> </div><!-- /.container-fluid --> </nav> <div class="row step1"> {% block content %} {% endblock %} //这里是后面除公共页面外你添加的html代码

</div> </body> </html>

 

2、抽取公共页面,添加自己新的html代码

{% extends 'common.html' %}

{% block content %}

这里插入自己要写的html

    
{% endblock %}

 

标签:抽取,--,Django,html,Link,公共,页面
来源: https://www.cnblogs.com/zq1994/p/16309563.html