当前位置:网站首页>Template inheritance and import
Template inheritance and import
2022-07-24 18:16:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Scenarios inherited by the template
situation 1: A template is usually used to frame the header of a page LOGO page , Left navigation menu , Only the content on the right is different . If you don't use a template, you do a lot of repetitive work .
Especially if the head or left navigation needs to be modified or added , All pages need to be modified .django Solve through template inheritance .
situation 2: If a page has a lot of content , It's impossible to write the same page together . For example, jd.com has a lot of content on its homepage . How to solve ?django adopt include Import other pages .One : Template inheritance
1) To use by inheritance
step 1: Write in motherboard block, Can be inherited ,content Is the name.
{% block title %}
{% endblock%}
{% block “content“ %}
{% endblock%}step 2: The sub page passes extends Specify which template to inherit
{% extends 'master.html'%} # Inherit that template
{% block “content“ %} This place is to replace the template block “content“
<ul>
{% for i in u%}
<li>{{i}}</li>
{%endfor%}
{% endblock%}2) If the sub page has its own css,js What's the use of ? A) If it is written on a sub page CSS and JS,CSS It's not on the head , and JS It's not <body> Before , If you want to quote jquery, The sub page is written JS Will be in jquery Quote from the front , Will not take effect
B) Inherit CSS And JS All in common .
resolvent :
It's in the template css and js The position is written block block . And then in block In the introduction , In this block Write your own js and css notes :block It has nothing to do with order
Two : Template introduction
3) A page can only inherit one template , How to solve ? How to use multiple templates , Or introduce other pages
<% include “a.html” %> You can quote multiple times
4) Templates ,include, How to render sub pages ? First render yourself into a string , Taking templates and include Rendering , So there is no rendering problem ( You can inherit sub pages include As a whole page )
3、 ... and : Example
#url.py
url(r'^tpl1$',views.tpl1),
url(r'^tpl2$',views.tpl2),
url(r'^tpl3$',views.tpl3),
#views.py
def tpl1(request):
u=[1,2,3]
return render(request,"tp1.html",{"u":u})
def tpl2(request):
name="alex"
return render(request,"tp2.html",{"name":name})
def tpl3(request):
status=" The modified "
return render(request,"tp3.html",{"status":status})
# modular :master.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
{% block title %}{% endblock %} <!-- This is used to set the title -->
</title>
<link rel="stylesheet" href="/static/common.css">
{% block css %}<!-- This is used to set the sub page's own css-->
{% endblock %}
</head>
<body>
{% block content %}<!-- This is used to set the content of the sub page -->
{% endblock %}
<script src="/static/js/jquery-1.12.4.js"></script>
<script src="/static/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
{% block js %}<!-- This is used to set the sub page's own js-->
{% endblock %}
</body>
</html>
# Subpage :tp1.html
{% extends "master.html" %} <!-- Inherit that template -->
{% block title %}
User management
{% endblock %}
{% block css %}
<style>
body{
background-color: aqua;
}
</style>
{% endblock %}
{% block content %}
<h1> User management </h1>
<ul>
{% for i in u %}
<li>{{ i }}</li>
{% endfor %}
</ul>
{% endblock %}
# Subpage :tp2.html
{% extends "master.html" %}
{% block content %}
<h1> Change Password {{ name }}</h1>
{% include "tp3.html" %} <!-- Introduce other pages -->
{% endblock %}
#include page :tp3.html
<div>
<input type="text">
<input type="button" value="++">
</div>Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/124460.html Link to the original text :https://javaforall.cn
边栏推荐
- 如何向 google colab 快速上传文件
- 运维小白成长记——架构第8周
- Polymorphism, abstract class, interface
- jmeter -- prometheus+grafana服务器性能可视化
- Codeforces Round #794 (Div. 2)(A.B.C)
- 头文件是必须的吗?跟一跟编译过程~~~
- 0625~<config>-<bus>
- How to follow the "low coupling" design principle?
- Three ways of redis cluster
- Section 11 cache avalanche, hot data failure follow Daewoo redis ------- directory post
猜你喜欢

手写博客平台~第二天

0630~职业素养课

下拉列表组件使用 iScroll.js 实现滚动效果遇到的坑

Go language interface and type

Pycharm configuring opencv Library
![[leetcode] 30. Concatenate substrings of all words](/img/21/3965532a31553cfe6edf64ca5de3f4.png)
[leetcode] 30. Concatenate substrings of all words

pycharm配置opencv库

Three ways of redis cluster

Interview assault 66: what is the difference between request forwarding and request redirection?

第五届数字中国建设峰会在福建福州开幕
随机推荐
A practical scheme of realizing 0.5px on mobile terminal
Mac database management software Navicat premium essentials mac
The collapse of margin
pinia 入门及使用
0621~ES&Lucene
PXE efficient batch network installation
数组扁平化.flat(Infinity)
仅需一个依赖给Swagger换上新皮肤,既简单又炫酷!
Baidu touch.js
pycharm配置opencv库
Icml2022 Best Paper Award: learning protein reverse folding from millions of predicted structures
Alibaba 1688 keyword search product API usage display
0613~自习
T245982 "kdoi-01" drunken flower Yin
Int8 & int8, have you ever stumbled like this?
Blackmagic Fusion Studio 18
Get the original data API on 1688app
0630~ professional quality course
猜JWT关键字
Alibaba /166 obtains the API instructions for all products in the store