template復用 extends block include render
參考:https://code.ziqiangxuetang.com/django/django-template.html
最基本的復用:
from django.shortcuts import render
return render(request,'hello.html',{'arg1':'val1'})
(1)撰寫基本模板(所有可重定義的地方用{% block 塊名 %}XXX{% endblock %}表示出來):
<!DOCTYPE html>
< html >
< head >
< title >{% block title %}默認標題{% endblock %} - 自強學堂</ title >
</ head >
< body >
{% include 'nav.html' %}
{% block content %}
<div>這里是默認內容,所有繼承自這個模板的,如果不覆蓋就顯示這里的默認內容。</div>
{% endblock %}
{% include 'bottom.html' %}
{% include 'tongji.html' %}
</ body >
</ html >
(2)繼承模板(開頭處聲明要繼承的模板{% extends 'html文件名' %},重寫處用{% block 塊名 %}XXX{% endblock %}表示出來):
{% extends 'base.html' %}
{% block title %}歡迎光臨首頁{% endblock %}
{% block content %}
{% include 'ad.html' %}
這里是首頁,歡迎光臨
{% endblock %}
(3)調用模板{% include '模板名' %}:
{% include 'ad.html' %}
