Python-Web: Django Template Inheritance
Templates can be reused through inheritance. Next, we first add a base.html file to the templates directory of the previous project, with the following code: !DOCTYPE html html head meta charset=utf-8 titleTitle/title /head body h1Hello World!/h1 pDA Django test./p {% block mainbody %
目录2 个章节
Templates can be reused through inheritance. Next, we first add a base.html file to the templates directory of the previous project, with the following code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>标题</title>
</head>
<body>
<h1>Hello World!</h1>
<p>DA Django 测试。</p>
{% block mainbody %}
<p>original</p>
{% endblock %}
</body>
</html>
In the code above, the block tag named mainbody is the part that can be replaced by inheritors. All {% block %} tags tell the template engine that child templates can override these parts. hello.html inherits from base.html and replaces a specific block. The modified code of hello.html is as follows:
{% extends "base.html" %}
{% block mainbody %}<p>继承了 base.html 文件</p>
{% endblock %}
The first line of code indicates that hello.html inherits the base.html file. As you can see, the block tag with the same name here is used to replace the corresponding block in base.html.
About
For more interesting operations, please follow my WeChat official account: DealiAxy Every article is collected on my blog: blog.deali.cn
_EOF_
Python-Web: Django Template Inheritance
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Python-Web-Django:settings-py-配置
安装REST框架 pip install djangorestframework pip install django-rest-swagger 安装了REST Framework之后,之前创建的Django App(hello_app)就可以使用REST API进行通信。 我们所有创建的App都要添加到INSTALLED_APPS这个字段里面。 INSTALLED_APPS = [ #
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web:Django-模板标签
if/else 标签 基本语法格式如下: {% if condition %} ... display {% endif %} 或者: {% if condition1 %} ... display 1 {% elif condition2 %} ... display 2 {% else %} ... display 3 {% endif %} 根据条件判断是否输出。
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web-Django:开始Django之旅
前言 开源的Web框架Django简单易用,稳定性和灵活性高,因此被广泛应用于商业化环境,它充分利用了Python拥有丰富的库这一优势。 Django的安装和服务器的搭建 安装Django库: pip install django 新建Web应用: django-admin startproject hello_django 以上命令会在当前文件夹下建立hello_django目录。 目录结构如下
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
Python-Web-Django:第一个Web-App的构建
View视图渲染 编辑App目录下的views.py from django.shortcuts import render from django.http import HttpResponse # Create your views here. def hello(request): return HttpResponse(Hello Django!) URL解析配置 编辑urls
2021-12-18 · 0_Python-Web-Django入门系列笔记 · 约 1 分钟
讨论
共 0 条加载评论中…
