使用环境同上篇django文章。
运行django服务:
]# cd py3/django-test1/test4 ]# python manage.py runserver 192.168.255.70:8000
创建html模板文件:
]# cd py3/django-test1/test4/templates/bookshop/ ]# vim base.html <!DOCTYPE html> <html> <head> <title>Title</title> {% block head %}{% endblock %} </head> <body> <h2>logo</h2> <hr> {% block content1 %} <h2>父模板--继承</h2> {% endblock %} <hr> <h2>contact</h2> </body> </html>
base.html为模板基础,让index2.html继承base.html:
]# vim index2.html {% extends 'bookshop/base.html' %}
编辑视图函数:
]# cd /root/py3/django-test1/test4 ]# vim bookshop/views.py from django.shortcuts import render from .models import * ... def index2(request): return render(request,'bookshop/index2.html')
添加应用url路由:
]# vim bookshop/urls.py from django.conf.urls import url from . import views urlpatterns = [ ... url(r'^index2$',views.index2, name='index2'), ]
浏览器访问:http://192.168.255.70:8000/index2
以上就是基本实现模板继承的示例演示。
再修改index2.html:
]# vim templates/bookshop/index2.html {% extends 'bookshop/base.html' %} {% block content1 %} <h2>this is a index2.html page!</h2> {% endblock content1 %}
说明:
block content1与父模板重名,则会覆盖继承的模板。
在结束标签中{% endblock content1%}可以添加名称。
访问浏览器:http://192.168.255.70:8000/index2
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
名称栏目:django之模板继承-创新互联
文章源于:https://www.cdcxhl.com/article16/diiogg.html
成都网站建设公司_创新互联,为您提供网站收录、标签优化、企业建站、定制开发、服务器托管、网站导航
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联