在 Django 模板中复制数据的最佳方法是什么? [英] Whats the best way to duplicate data in a django template?

查看:34
本文介绍了在 Django 模板中复制数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这或多或少是我的模板.h1 标题始终与标题标签相同.上面的代码片段无效,因为不能有两个同名的块.我该如何处理而不重复自己?

This is my template, more or less. The h1 heading is always the same as the title tag. The above snippet of code is not valid because there can't be two blocks with the same name. How do I handle this without repeating myself?

编辑澄清:我有大量从这个模板继承的子模板,因此为每个模板创建一个新的 {{title}} 变量并不是一个很好的解决方案.以前我是这样设置的:

edit to clarify: I have a ton of child templates which are inherited from this one template, and so making a new {{title}} variable for each template is not a very good solution. Previously I had it set up like this:

base.html:

<title>{% block title %}{% endblock %}</title>

然后在 base_view.html(扩展 base.html)中:

then in base_view.html (extending base.html):

<h1>{% block title %}{% endblock %}</h1>

然后在 base_object.html(扩展 base_view.html)中:

then in base_object.html (extending base_view.html):

{% block title %}my title goes here{% endblock %}

它只是以某种方式起作用.我重构了我的模板,所以只有 base.html 和 base_object.html 我怎样才能恢复这个功能?

and it just worked somehow. I refactored my templates so theres just base.html, and base_object.html How can I get this functionality back?

推荐答案

看起来您的布局很稳固.您有一个 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中定义一个像Stackoverflow.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 "Stackoverflow.com" in the base, and set

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

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

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

这篇关于在 Django 模板中复制数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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