如何使用带有FilterView mixin的类View处理django-tables2上的提交

时间:2019-07-29 06:36:56

标签: django django-tables2

我有一个django-tables2 FilterView。 过滤器以以下形式模板化:

{% if filter %}
    <form action="" method="get" class="form form-inline">
        {% bootstrap_form filter.form layout='inline' %}
        {% bootstrap_button 'filter' %}
    </form>
{% endif %}

我在每一行中添加了一个复选框字段,并且该表具有以下形式:

<form action="table_selection" method="get" class="form form-inline">
    {% csrf_token %}
    {% render_table table 'django_tables2/bootstrap.html' %}

    <button class="btn btn-primary red" type="submit" >Submit Rows</button>
</form>

提交后,我收到如下日志消息:

GET /three_pl/threepl_fulfilments_filter/table_selection?csrfmiddlewaretoken=...
&select_row=198&select_row=158&select_row=159 

所以select_rows非常有趣。 但是我迷失了类视图,我无法解决如何处理表单提交的问题。

这是我的观点:

class FilteredThreePLFulfimentsView(SingleTableMixin,FilterView):
    table_class = ThreePL_order_fulfilmentsTable
    model = ThreePL_order_fulfilments
    template_name = "three_pl/ThreePLFulfilmentsFilter.html"  #three_pl/templates/three_pl/ThreePLFulfilmentsFilter.html
    filterset_class = ThreePLFulfilmentsFilter

1 个答案:

答案 0 :(得分:0)

答案:设置一个监视/ table_selection的URL。 将其设置为表格呈现网址之前。

...    url(r'^threepl_fulfilments_filter/table_selection',views.three_pl_fulfilments_selection,name='threepl_fulfilments_selection'),
...

并在视图中:

if request.method == 'GET':
    try:
        selected_rows = request.GET.getlist('select_row')