且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Django 模板中复制数据的***方法是什么?

更新时间:2023-02-09 15:55:10

看起来您的布局很稳固.您有一个 base.html 模板,用于定义应用中每个页面的基本结构和外部布局.您还有扩展此模板的 base_object.html.

It looks like your layout is solid. You have a base.html template that defines the basic structure and outer layout for each page in your app. You also have base_object.html that extends this template.

您希望每个页面都有唯一的标题和匹配的 h1(我认为).***的方法是在 base.html 模板中定义两个单独的块.

You'd like each page to have a unique title and a matching h1 (I think). This best way to do this is to define two separate blocks in your base.html template.

<html>
    <head>
        <title>{% block title %}Default Title{% endblock %}</title>
    </head>

    <body>
        <h1>{% block h1 %}{% endblock %}</h1>
    </body>
</html>

在您的子模板中,如果您希望它们相同,则需要覆盖这两个模板.我知道你觉得这违反直觉,但由于 Django 中处理模板继承的方式,这是必要的.

In your child templates, you need to override both of these if you'd like them to be identical. I know you feel this is counter-intuitive, but it is necessary due to the way template inheritance is handled in Django.

来源:Django 模板语言

最后,注意不能在同一个模板中定义多个同名的 {% block %} 标签.存在此限制是因为块标记在双向"方向上都起作用.也就是说,块标记不仅提供一个要填充的孔——它还定义了填充父级中的孔的内容.如果模板中有两个名称相似的 {% block %} 标记,则该模板的父级将不知道要使用哪个块的内容.

Finally, note that you can't define multiple {% block %} tags with the same name in the same template. This limitation exists because a block tag works in "both" directions. That is, a block tag doesn't just provide a hole to fill -- it also defines the content that fills the hole in the parent. If there were two similarly-named {% block %} tags in a template, that template's parent wouldn't know which one of the blocks' content to use.

孩子们看起来像这样:

{% extends "base.html" %}
{% block title %}Title{% endblock %}
{% block h1 %}Title{% endblock %}

如果这让您感到困扰,您应该将每个对象的视图标题设置为模板变量.

If this bothers you, you should set the title from the view for each object as a template variable.

{% block title %}{{ title }}{% endblock %}
{% block h1 %}{{ title }}{% endblock %}

Django 努力将尽可能多的逻辑排除在模板层之外.通常,标题是从数据库动态确定的,因此视图层是检索和设置此信息的理想场所.如果您想遵循默认标题(可能在 base.html 中设置,或者您可以从 django.contrib 中获取站点名称),您仍然可以将标题留空.sites 包)

Django strives to keep as much logic out of the template layer as possible. Often a title is determined dynamically from the database, so the view layer is the perfect place to retrieve and set this information. You can still leave the title blank if you'd like to defer to the default title (perhaps set in base.html, or you can grab the name of the site from the django.contrib.sites package)

还有 {{ block.super }} 可能会派上用场.这将允许您将父块的内容与子块的其他内容结合起来.所以你可以在base中定义一个像***.com"这样的标题,然后设置

Also {{ block.super }} may come in handy. This will allow you to combine the contents of the parent block with additional contents from the child. So you could define a title like "***.com" in the base, and set

{% block title %}{{ block.super }} - Ask a Question{% endblock %}

在孩子中获得类似***.com - Ask a Question"之类的标题

in the child to get a title like "***.com - Ask a Question"