在Django中一次提交两个表单

时间:2018-05-09 20:45:28

标签: python html django django-templates

我正在处理import numpy as np def f(data, t): x = data['focus']*data['integration']*data['upper'] return t*x def Trial(t, temp, data): calcdata = {} counter = 0 keys = temp.keys() for value0 in temp[keys[0]]: for value1 in temp[keys[1]]: for value2 in temp[keys[2]]: data[keys[0]] = value0 data[keys[1]] = value1 data[keys[2]] = value2 x_axis, y_axis = np.vstack((t,np.vectorize(f)(data,t))) calcdata[counter] = {} calcdata[counter]['xaxis'] = x_axis calcdata[counter]['yaxis'] = y_axis calcdata[counter][keys[0]] = value0 calcdata[counter][keys[1]] = value1 calcdata[counter][keys[2]] = value2 counter+=1 return calcdata data['focus'] = 100 t = np.linspace(1,10,10) temp = {'integration': [1,2], 'upper': [3,4], 'other': [5,6]} calcdata = Trial(t, temp, data) ,其中包括两种形式,搜索表单和过滤器表单。这两种形式最终都会通过其ListView方法更改视图的queryset。我想这样做,当你搜索某些东西时,过滤器是持久的,反之亦然。

它或多或少有效,但仍有一个错误:每当我搜索某些内容时,会出现一个“清除过滤器”按钮,该按钮不应该出现在那里:

enter image description here

在模板中,搜索表单包含如下:

get_queryset

其中{% block search_form %} {% with action='dashboard:families' placeholder='Search Families' %} {% include '_search.html' %} {% endwith %} {% endblock %}

_search.html

自定义过滤器{% load get %} <form action="{% url action %}" method="get" class="left search col s6 hide-on-small-and-down" novalidate> <div class="input-field"> <input id="search" placeholder="{{ placeholder }}" autocomplete="off" type="search" name="q" value="{{ search_form.q.value.strip|default:'' }}" data-query="{{ search_form.q.value.strip|default:'' }}"> <label for="search" class="active"><i class="material-icons search-icon">search</i></label> <i data-behavior="search-clear" class="material-icons search-icon" {% if not search_form.q.value %}style="display: none;"{% endif %}>close</i> </div> {% if filter_form %} {% for field in filter_form %} <input type="hidden" name="{{ field.name }}" value="{{ request.GET|get:field.name }}"/> {% endfor %} {% endif %} </form> 只是实现了get方法:

dict.get

简而言之,搜索表单包含一个隐藏的from django import template register = template.Library() @register.filter def get(dictionary, key): return dictionary.get(key) ,它随搜索查询一起提交。

包含过滤器表单的模板部分如下所示:

filter_form

请注意,过滤器表单还包含一个带有搜索查询的名为 <form action={% url 'dashboard:families' %} method="GET" data-behavior="filters"> <input type="hidden" name="q" value="{{ request.GET.q.strip }}"/> <div class="input-field col s2"> {{ filter_form.guide }} <label class="active">Guide</label> {% if filter_form.is_guide_filled %} <a href="" class="clear"><i class="material-icons tiny">clear</i></a> {% endif %} </div> <div class="input-field col s2"> {{ filter_form.status }} <label class="active">Status</label> {% if filter_form.is_status_filled %} <a href="" class="clear"><i class="material-icons">clear</i></a> {% endif %} </div> <div class="input-field col s2"> {{ filter_form.package }} <label class="active">Company / Package</label> {% if filter_form.is_package_filled %} <a href="" class="clear"><i class="material-icons tiny">clear</i></a> {% endif %} </div> <div class="input-field col s2"> {{ filter_form.next_outreach }} <label>Outreach</label> {% if filter_form.is_next_outreach_filled %} <a href="" class="clear"><i class="material-icons tiny">clear</i></a> {% endif %} </div> <div class="input-field col s2"> {{ filter_form.country }} <label class="active">Country</label> {% if filter_form.is_country_filled %} <a href="" class="clear"><i class="material-icons tiny">clear</i></a> {% endif %} </div> <div class="input-field col s1"> {{ filter_form.vip }} <label>VIP</label> {% if filter_form.is_vip_filled %} <a href="" class="clear"><i class="material-icons tiny">clear</i></a> {% endif %} </div> <div class="input-field col s1"> {{ filter_form.app }} <label>App</label> {% if filter_form.is_app_filled %} <a href="" class="clear"><i class="material-icons tiny">clear</i></a> {% endif %} </div> <a href="{% url 'dashboard:families' %}?q={{ request.GET.q.strip }}" data-behavior="clear-filters" class="btn-flat" {% if not filter_form.is_filled %}style="display: none;"{% endif %}> <i class="material-icons">close</i>Clear Filters </a> </form> 的隐藏字段。由于这是q,因此将CharField设置为value是有意义的。

问题在于,如果我放入request.GET.q.strip中的调试器,我发现表单的数据是我期望它的“字符串化”版本:

FamilyFilterForm

请注意ipdb> self.data <QueryDict: {'q': ['Christine'], 'status': ['None'], 'next_outreach': [''], 'country': ['None'], 'vip': [''], 'app': [''], 'guide': ['6'], 'package': ['None']}> 值是如何转换为字符串None的。这会导致过滤器表单的'None'属性“认为”已填写值,而实际上没有。

相反,我希望表单的数据等同于

is_filled

,特别是,如果没有应用过滤器,则应该只是空ipdb> from django.http import QueryDict ipdb> query_dict = QueryDict('q=Christine&guide=6') ipdb> query_dict.dict() {'q': 'Christine', 'guide': '6'} QueryDict()

如果以这种方式在模板中作为{}传递时,如何避免request.GET值变为'字符串化'?

1 个答案:

答案 0 :(得分:0)

我设法通过使用字段的as_hidden属性正确地包含隐藏的filter_form(正确表示了多个选择字段)。因此_search.html模板变为

<form action="{% url action %}" method="get" class="left search col s6 hide-on-small-and-down" novalidate>
  <div class="input-field">
    <input id="search" placeholder="{{ placeholder }}"
        autocomplete="off" type="search" name="q"
        value="{{ search_form.q.value.strip|default:'' }}"
        data-query="{{ search_form.q.value.strip|default:'' }}">
    <label for="search" class="active"><i class="material-icons search-icon">search</i></label>
    <i data-behavior="search-clear"
        class="material-icons search-icon"
        {% if not search_form.q.value %}style="display: none;"{% endif %}>close</i>
  </div>
  {% for field in filter_form %}
    {{ field.as_hidden }}
  {% endfor %}
</form>

同样,过滤器表单包含

{{ search_form.q.as_hidden }}

保留搜索查询。