我正在尝试在我的一个模板中实现两个下拉菜单,但我不断收到此错误。
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
57. return view_func(*args, **kwargs)
File "views.py" in doclistings
91. request.session["genderselect"] = request.POST['genderselect']
File "/Library/Python/2.7/site-packages/django/utils/datastructures.py" in __getitem__
301. raise MultiValueDictKeyError(repr(key))
Exception Type: MultiValueDictKeyError at /doclistings/
Exception Value: "'genderselect'"
这是一个模板,我试图在一个单独的表单中使用两个下拉菜单
<form action="" method="post" >
<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>
以下是我收到错误的视图
def doclistings(request):
d = getVariables(request)
if request.method == "POST":
form = DropdownSelectionForm(request.POST)
print form.errors
if form.is_valid():
selection = form.cleaned_data['selection']
genderselect = form.cleaned_data['genderselect']
d['usergendselect'] = genderselect
request.session["selection"] = request.POST['selection']
return HttpResponseRedirect('/doclistings')
else:
form = DropdownSelectionForm()
s_name = request.session.get('selection') # Change variable name
d['userselection'] = s_name # Update this for new variable name
gend = Doctor.objects.get(gender = genderselect)
spec = Specialization.objects.get(name=s_name) # Get spec object
doctors = Doctor.objects.filter(specialization = spec, gender = gend).order_by('-likes')
d['doctors'] = doctors
d.update({'form': form})
return render_to_response('meddy1/doclistings.html',d)
这是Doctor模型
class Doctor(models.Model):
name = models.CharField(max_length=100)
specialization = models.ForeignKey(Specialization)
clinic = models.ForeignKey(Clinic)
seekers = models.ManyToManyField(User, through='UserContent')
gender_choices = ( ('Male', 'Male'), ('Female','Female'),)
gender = models.CharField(max_length=15, choices = gender_choices, null=True, blank = True)
image = models.ImageField(upload_to='uploads/', null=True, blank = True)
mimetype = models.CharField(max_length=20)
likes = models.IntegerField(default=0)