django模板复用 extends,block,include


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' %} 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM