【问题标题】:Why does django_tables2 render the same table twice?为什么 django_tables2 会两次渲染同一张表?
【发布时间】:2017-11-16 04:32:08
【问题描述】:

我正在使用 django_tables2 创建一些表。我的应用程序被称为“导演”,我有一个基本模板,然后是一个继承自它的子模板。代码大部分按预期工作——子模板正确地从父模板继承了 divss 和东西,并且样式是正确的,但是子模板也第二次输出同一个表,这次没有父模板的样式。我一生都无法弄清楚为什么会生成第二个表。

这是基本模板。

#director_table.html
{% block content %}
<div class="container">
  <div class="row">
    <div class="col-lg-12 mb-4 mt-4">
        <div class="card h-100">
            <div class="card-header">List</div>
            <div class="card-body">
            <h4 class="card-title"></h4>
            <h6 class="card-subtitle"></h6>

                {% block table_stuff %}

                {% endblock %}
            </div>
        </div>
    </div>
 </div>
</div>
{% endblock %}

这是孩子。

{% extends 'director/director_table.html' %}
    {% load render_table from django_tables2 %}
    {% block content %} 
    {{ block.super }}

        {% block table_stuff %}
            {% render_table table %}
        {% endblock %}

    {% endblock %}

编辑

如果我注释掉 {% render_table table %} 行,那么我得到 0 个表,而不是 2 个。

【问题讨论】:

    标签: django django-tables2


    【解决方案1】:

    解决了。解决方案是将 css/bootstrap 内容划分为更多块。

    父模板:

    {% block content %}
        {% block top %}
    <div class="container">
        <div class="row">
            <div class="col-lg-12 mb-4 mt-4">
                <div class="card h-100">
                    <div class="card-header">List</div>
                    <div class="card-body">
                    <h4 class="card-title"></h4>
                    <h6 class="card-subtitle"></h6>
        {% endblock %}
                        {% block table_stuff %}
    
                        {% endblock %}
        {% block bottom %}
                    </div>
                </div>
            </div>
        </div>
    </div>
        {% endblock %}
    {% endblock %}
    

    和孩子:

    {% extends 'director/director_table.html' %}
    {% load render_table from django_tables2 %}
    {% block content %}
        {% block top %}
        {{ block.super }}
        {% endblock %}
        {% block table_stuff %}
            {% render_table table %}
        {% endblock %}
        {% block bottom %}
        {{block.super}}
        {% endblock %}
    {% endblock %}
    

    这给出了预期的结果。

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 2023-01-24
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 2020-03-17
      • 2020-07-11
      相关资源
      最近更新 更多