在Django中,{%block content%}和{%endblock content%}分别代表什么? [英] what is {% block content %} and {% endblock content %} for in Django?

查看:178
本文介绍了在Django中,{%block content%}和{%endblock content%}分别代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我刚开始读一本关于Django的书(面向初学者),我遇到了以下代码片段:

so I just started reading a book on Django (for beginners) and I came across the following code snipet:

<header>

<a href="{% url 'home' %}">Home</a> | <a href="{% url 'about' %}">About</a>

</header>

{% block content %}
{% endblock content %}

谁能向我解释 {%块内容%} {%块内容%}

推荐答案

block 用于重载!模板的特定部分。

block is used for overriding specific parts of a template.

在您的情况下,您有一个名为 content 的块,应该由继承自

In your case, you have a block named content and this is supposed to be overridden by children that inherit from this template.

来自 Django文档

要扩展的模板,名为 base.html

<head>
    <link rel="stylesheet" href="style.css">
    <title>{% block title %}My amazing site{% endblock %}</title>
</head>

覆盖子模板

{% extends "base.html" %}

{% block title %}My amazing blog{% endblock %}

孩子会覆盖我的惊人站点,然后显示我的惊人博客

"My amazing site" will be overriden by the child and then display "My amazing blog"

这篇关于在Django中,{%block content%}和{%endblock content%}分别代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆