1. include 法宝
- 提取公共部分代码放入特定的一个html文件中
- 用include标记来引入重复的文件的部分
//views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader,Context
# Create your views here.
def base(request):
t=loader.get_template("base.html")
return HttpResponse(t.render({}))
//urls.py
from django.contrib import admin
from django.urls import path
from third_app import views
urlpatterns = [
path("admin/", admin.site.urls),
path("base/",views.base),
]
然后再third_app下建立一个templates的包,在包中建立copyright.html和base.html
<p align="center">Copyright © 2022,Power by Django</p>
{% include 'copyright.html' %}
2. django 模板的继承
1. {% extends "***.html" %}
2. {% block var_name %}***{% endblock %}
可以变的内容通过以下方式进行修改
//home.html
{% extends 'base.html' %}
{% block title %} home page {% endblock %}
{% block content %}
<p align="center"> hello this is home page !</p>
{% endblock%}
同理,修改以下代码,可以得到
- include 的使用
- django模板继承方法
网友评论