Django-tables2将参数发送到自定义表格模板 [英] Django-tables2 send parameters to custom table template

查看:57
本文介绍了Django-tables2将参数发送到自定义表格模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用自定义表格模板将 django-filter 字段嵌入到我的表格中.因此,我将 django-tables2 bootstrap.html 模板复制到了新文件 custom_table.html 中.然后,我在 thead 部分中将以下代码添加了:

I'm trying to use a custom table template to embed the django-filter fields on my table. So I copied the django-tables2 bootstrap.html template in a new file custom_table.html. Then I added it the following code in the thead section:

     {% if filter %}
       <tr>
          {% for filter_field in filter.form.fields %}
              <td>
                 {{ filter_field }}
              </td>
          {% endfor %}
          <td>
             <button class="login100-form-btn" type="submit">Filter</button>
          </td>
       </tr>
    {% endif %}

问题是:如何将 filter 发送到表模板?

So the problem is : how can I send the filter to the table template?

推荐答案

我解决了此问题.我已经覆盖了视图的 get_context_data 函数:

I resolved this issue. I have overridden the get_context_data function of my view:

    def get_context_data(self, **kwargs):
   
         context = super().get_context_data(**kwargs)
         table = self.get_table(**self.get_table_kwargs())
         table.filter = self.filterset
         context[self.get_context_table_name(table)] = table
         return context

通过这种方式,我可以通过以下代码在自定义表格模板中使用过滤器:

In this way, I can use the filter in my custom table template by the following code:

               {% if table.filter %}

                <tr>
                    <form action="" method="get" class="form-inline">
                        {% csrf_token %}
                        {% for field_form in table.filter.form %}
                        <th>
                            {{field_form}}
                        </th>
                        {% endfor %}
                        <th>
                            <button class="btn" type="submit">Filter</button>
                        </th>
                    </form>
                </tr>
                {% endif %}

这篇关于Django-tables2将参数发送到自定义表格模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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