【发布时间】:2014-06-21 08:39:25
【问题描述】:
我在一个模板中有两个下拉菜单。他们都使用相同的表格。但由于某种原因,只有一种形式有效,并在视图中给了我MultiValueDictKeyError。它给了我request.session["genderselect"] = request.POST['genderselect']线上的错误
所以我已经注释掉了这些行,看看会发生什么,代码可以工作并显示第一个下拉列表(名称 = 选择)。但是 (name = genderselect) 的第二个下拉列表不起作用,尽管它们都是同一表单的一部分。
views.py
def doclistings(request):
d = getVariables(request)
if request.method == "POST":
form = DropdownSelectionForm(request.POST)
if form.is_valid():
print form.errors
selection = form.cleaned_data['selection']
# genderselect = form.cleaned_data['genderselect']
# request.session["genderselect"] = request.POST['genderselect']
request.session["selection"] = request.POST['selection']
return HttpResponseRedirect('/doclistings')
else:
form = DropdownSelectionForm()
# d['genderselect'] = genderselect
s_name = request.session.get('selection')
d['userselection'] = s_name
spec = Specialization.objects.get(name=s_name)
doctors = Doctor.objects.filter(specialization = spec).order_by('-likes')
d['doctors'] = doctors
d.update({'form': form})
return render_to_response('meddy1/doclistings.html',d)
forms.py
class DropdownSelectionForm(forms.Form):
selection = forms.ChoiceField(choices=MY_CHOICES, widget = forms.Select, required = False)
genderselect = forms.ChoiceField(choices=GENDER_CHOICES, widget= forms.Select, required = False)
这是我有两个下拉菜单的模板
<select class="form-control" id="selection" name="selection">
<option><b>Find a Doctor...</b></option>
{% for value, text in form.selection.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
<select class="form-control" id="genderdropdown" name="genderdropdown">
<option><b>Select a Gender</b></option>
{% for value, text in form.genderselect.field.choices %}
<option value="{{ value }}">{{ text }}</option>
{% endfor %}
</select>
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Search</button>
</span>
</div>
{% csrf_token %}
</form>
【问题讨论】:
标签: python django forms drop-down-menu