简体   繁体   English

Django DRY - 如何在 Django 模板中扩展两个 .html 文件?

[英]Django DRY - How to extend two .html files inside a Django Template?

What do I want?我想要什么?

I want to extend cards/apps.html inside addstudents.html so that I don't need to write the chucks of codes multiple times.我想在addstudents.html扩展cards/apps.html ,这样我就不需要多次编写代码。 What can I do to extends multiple .html files inside DJANGO template?如何在 DJANGO 模板中扩展多个.html文件?

Error I am getting我得到的错误

'extends' cannot appear more than once in the same template 'extends' 不能在同一个模板中出现多次

WHAT TO DO?该怎么办?

NOTE: I don't want to use {% include %} , because the situation/condition isn't suitable.注意:我不想使用{% include %} ,因为情况/条件不合适。

SOME INFORMATION YOU MAY NEED...您可能需要的一些信息...

Inside cards/apps.html内卡/apps.html

<div class="card shadow mb-4">
<div class="card-header py-3">
    <h6 class="m-0 font-weight-bold text-primary">{{card_title}}</h6>
</div>
<div class="card-body">
    {% block card_body %}
    
    {% endblock %}
</div>

Inside addstudents.html里面addstudents.html

{% extends 'layouts/panel.html' %}
{% extends 'apps/card.html' %}
{% load static %}
{% block content %}

{% with card_title='My Card TITLE' %}
{% block card_body %}
...SOME FORM, .... SOME PARAGRAPH
{% endblock %}

{% endblock %}

What's inside layouts/panel.html layouts/panel.html 里面有什么

layouts/panel.html contains some menu and navbars items [including CSS, bootstrap and JS dependencies]. layouts/panel.html包含一些菜单和导航栏项目 [包括 CSS、引导程序和 JS 依赖项]。

What's apps/card.html ?什么是apps/card.html

apps/card.html contains the code-snippet of HTML and Bootstrap CARD. apps/card.html包含 HTML 和 Bootstrap CARD 的代码片段。 And I don't want to write this code multiple times.而且我不想多次编写此代码。 That's why I want it to be manipulated via Django Template Tags .这就是为什么我希望它通过Django Template Tags进行操作。

HOPE YOU UNDERSTOOD希望你明白

I don't think this is directly possible.我不认为这是直接可能的。 But here is a work around.但这里有一个解决方法。 In the view which handles addstudents.html , add has_card to the context as True .在处理addstudents.html的视图中,将has_card添加到上下文中为True

views.py

def app_students(request):
    # Your code
    return render(request, 'addstudents.html', {'has_card': True})

layouts/panel.html

[...]
{% if has_card %}
<div class="card shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">{{card_title}}</h6>
    </div>
    <div class="card-body">
        {% block card_body %}

        {% endblock %}
    </div>
</div>
{% endif %}
[...]

addstudents.html

{% extends 'layouts/panel.html' %}
{% with card_title='My Card TITLE' %}
{% block card_body %}
...SOME FORM, .... SOME PARAGRAPH
{% endblock %}

So this, if has_card doesn't exist, won't have the card_body block, but if it does exist as a truthy value, then it will.因此,如果has_card不存在,则不会有card_body块,但如果它确实作为真值存在,那么它就会存在。

I'm not sure if it fits your need.我不确定它是否适合您的需要。 One way could be to split the responsibilities a bit between the different templates.一种方法是在不同的模板之间稍微划分职责。 From given information it could be possible to put bootstrap/layout stuff in cards/apps.html and add additional blocks for childs to override to just handle the forms and view specific information.根据给定的信息,可以将引导程序/布局内容放在cards/apps.html 中,并为子项添加额外的块以覆盖以仅处理表单和查看特定信息。

To clarify the inheritance that may resolve the problem:澄清可能解决问题的继承:

layouts/panel.html -> apps/card.html -> addstudents.html布局/panel.html -> 应用程序/card.html -> addtudents.html

The apps/card.html overrides block content from layout/panel.html and adds additional blocks which are used by the addstudents.html . apps/card.html覆盖layout/panel.html block content并添加由addstudents.html使用的附加块。

It can also be seen as, in words: addstudents.html extends card.html which extends panel.html.它也可以被看成是:addstudents.html 扩展了 card.html,它扩展了 panel.html。 Afaik you can extend as many times you want, but not use extends twice in the same template. Afaik 您可以根据需要进行多次扩展,但不能在同一模板中使用 extends 两次。

cards/apps.html卡片/apps.html

{% extends 'layouts/panel.html' %}
{% block content %}
    <div class="card shadow mb-4">
    <div class="card-header py-3">
        <h6 class="m-0 font-weight-bold text-primary">{% block card_title %}{% endblock %}</h6>
    </div>
    <div class="card-body">
        {% block card_body %}
        
        {% endblock %}
    </div>
{% endblock content %}

addstudents.html addstudents.html

{% extends 'apps/card.html' %}

{% block card_title}My Card TITLE{% endblock card_title%}
{% block card_body %}
...SOME FORM, .... SOME PARAGRAPH
{% endblock card_body %}

Worth mention is also {{ block.super }} which can be quite useful sometimes if you need to render a "parent".值得一提的是{{ block.super }}如果你需要渲染一个“父”,它有时会非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM