一、模板继承
目的:减少代码的冗余
语法:
|-------|------------------------------------------|
| 1 2 3 | {% block classinfo %}
{% endblock %}
|
具体步骤:
1、创建一个base.html文件
2、把要显示的页面的内容写在这里面,也就是HTML要在浏览器显示的内容
3、在rigth里面写一个盒子
{% block classinfo %}
{% endblock %}
在这里写一个空盒子,以后谁来扩展就在这个盒子里面添加相应的内容就行了
4、然后再创建一个.html文件,让这个继承base.html文件
{% extends 'base.html' %} # 必须是在文件的第一行
在基版里面添加内容
{% block classinfo %}
<h2>首页</h2>
<h2>学生信息</h2>
<h3>{{ class_id }}班</h3>
{% endblock %}
5、也可以写好多盒子,
在left中写个盒子
{% block menu %}
<p>I see you you</p>
{% endblock %}
注意:
盒子里面可以有默认的内容,如果有默认的时候你不扩展就走默认的,如果你扩展了,就替换了,
那么不替换直接追加可以嘛?可以的,那就用下面的方式。
{% block.super %}
|---------------|--------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 | {% block menu %}
{``{ block.super }}
<``p``>!!!</``p``> #先继承父类的,后插入数据
{% endblock %}
|
1、模板继承围绕两点:继承和扩展
你有什么继承什么,
扩展的是盒子,
2、模板中设置的盒子越多越好,因为这样你想扩展的时候就容易了。我想扩展就扩展了。不扩展就不扩展了
3、为了更好的可读性,你也可以给你的 {% endblock %}
标签一个 名字 。例如:
|-------|-------------------------------------------------------|
| 1 2 3 | {% block content %}
...
{% endblock content %}
|
4、如果你发现你自己在大量的模版中复制内容,那可能意味着你应该把内容移动到父模版中的一个 {% block %}
中。
最后,请注意你并不能在一个模版中定义多个相同名字的 block
标签。这个限制的存在是因为block标签的作用是"双向"的。这个意思是,block标签不仅提供了一个坑去填,它还在 _父模版_中定义了填坑的内容。如果在一个模版中有两个名字一样的 block
标签,模版的父模版将不知道使用哪个block的内容。
具体例子说明
|---|---------------------------------------|
| 1 | re_path(r'^text/(\d+)', views.text)
|
|-----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 | def text(request, class_id):
``# 班级变量
``print(class_id) # 拿到的是你在路径里输入的几就是几
``# 数据库查询
``return render(request, "text.html", {"class_id": class_id})
|
templatite/base.html
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | <!``DOCTYPE
html>
<``html
lang="en">
<``head``>
``<``meta
charset="UTF-8">
``{% block title %}
``<``title``>base</``title``>
``{% endblock %}
``<!--最新版本的bootstrap核心CSS文件-->
``<``link
rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
``integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
``<``title``>My amazing site</``title``>
``<``style``>
``* {
``margin: 0;
``padding: 0;
``}
``.header {
``width: 100%;
``height: 50px;
``background-color: #369;
``}
``.right {
``height: 400px;
``background-color: silver;
``}
``</``style``>
</``head``>
<``body``>
{# 导航条 #}
<``nav
class="navbar navbar-primary navbar-inverse">
``<``div
class="container-fluid">
``<``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="#">Brand</``a``>
``</``div``>
``<!--Collect the nav links,forms, and other content for togging-->
``<``div
class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
``<``ul
class="nav navbar-nav">
``<``li
class="active"><``a
href="#">Link <``span
class="sr-only">(current)</``span``> </``a``> </``li``>
``<``il``><``a
href="#">Link</``a``> </``il``>
``<``li
class="dropdown">
``<``a
href="#" class="dropdown-toggle" data-toggle="drodown" role="button" aria-haspopup="true"
``aria-expanded="false">Dropdown <``span
class="caret"></``span``> </``a``>
``<``ul
class="dropdown-menu">
``<``li``><``a
href="#">Action</``a``> </``li``>
``<``li``><``a
href="#">Another action</``a``> </``li``>
``<``li``><``a
href="#">Something else here</``a``> </``li``>
``<``li
role="separator" class="divider"></``li``>
``<``li``><``a
href="#">Separated link</``a``> </``li``>
``<``li
role="separator" class="divider"></``li``>
``<``li``><``a
href="#">One more separated link</``a``> </``li``>
``</``ul``>
``</``li``>
``</``ul``>
``</``div``>
``</``div``>
</``nav``>
{# 内容 #}
<``div
class="containers">
``<``div
class="row">
``<``div
class="col-md-11 col-lg-offset-1">
``<``div
class="left col-md-3">
``{% block menu %}
``<``div
class="list-group">
``<``a
href="#" class="list-group-item active">
``学生班级
``</``a``>
``<``a
href="/text/6" class="list-group-item">s6</``a``>
``<``a
href="/text/7" class="list-group-item">s7</``a``>
``<``a
href="/text/8" class="list-group-item">s8</``a``>
``<``a
href="/text/9" class="list-group-item">s9</``a``>
``</``div``>
``{% endblock %}
``</``div``>
``<``div
class="right col-md-8">
``{# 定义一个盒子#}
``{% block classinfo %}
``{% endblock %}
``</``div``>
``</``div``>
``</``div``>
</``div``>
</``body``>
</``html``>
|
template/text.html继承上面的文件
|-------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | {% extends "base.html" %}
{% block classinfo %}
``<``h1``>学生信息</``h1``>
``<``h3``>{``{ class_id }}</``h3``>
{% endblock %}
{#追加#}
{% block menu %}
``{``{ block.super }}
``<``a
href="">学生信息</``a``>
{% endblock %}
|
二、静态文件配置
我们自己导入的一些包就叫做静态文件
1、在全局中先创建一个static的包,
2、在static里面导入我们的bootstrap,还是jquery等
3、然后在settings.py中加上一些配置
|---------|-----------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 | STATIC_URL = '/static/' #这个配置就相当于下面配置的别名,如果这里的名字修改了就按照这里的名字去导入
STATICFILES_DIRS = [
``os.path.join(BASE_DIR,"static")
]
|
4、导入css,js,jquery
|-------|--------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 | <``link
rel = "stysheet",href= "/static/index.css/">
<``link
rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
|