我希望获得用户选择的下拉字段的值,并在下一页使用它来过滤一些概要文件列表,基于下拉选择。
在我的index.html页面中有这样的内容,希望用户在下拉列表中选择一个选项,然后点击submit,然后重定向到doclistings.html页面。
<div class="form-group">
<form action="/doclistings/" method="post">
<select class="form-control" id="select">
<option><b>Choose a Speciality...</b></option>
<option value ="Dermatologist">Dermatologist</option>
<option value = "Dentist">Dentist</option>
<option value = "ENT">Ear, Nose and Throat (ENT)</option>
<option value = "Opthalmologist">Eye Doctor</option>
<option value = "Psychiatrist">Psychiatrist</option>
<option value = "Orthopedist">Orthopedist</option>
</select>
<span class="input-group-btn">
<button class="btn btn-primary" type="submit" name="submit" id="ss-submit">Find Doctors</button>
</span>
</div>doclistings.html
{% for doc in doclist %}
{% if doc.specialization == } # value from the last page
<div class = "doctorrow">
<h2><a href="/docprofile/{{doc.id}}/">{{doc.name}}</a></h2>
<h3> {{doc.specialization}}</h3>
</div>
</div>
{% endfor %}views.py
def index(request):
if request.method == "POST":
form = DropdownSelectionForm(request.POST)
if form.is_valid():
selection = form.cleaned_data['value']
return HttpResponseRedirect('/doclistings')
else:
form = DropdownSelectionForm()
return render(request, 'meddy1/index.html')
@csrf_exempt
def doclistings(request):
return render(request, 'meddy1/doclistings.html', {'doclist': Doctor.objects.all()})forms.py
class DropdownSelectionForm(forms.Form):
selection = forms.CharField()发布于 2014-06-10 08:19:29
尝试POST,然后使用来自request的POST值在下一个视图中呈现它。这是医生-
https://docs.djangoproject.com/en/1.6/topics/forms/#using-a-form-in-a-view
编辑您可以执行以下任何一项操作-
form action,并将form method设置为GET。因此,当表单发布时,您可以在alldocs视图的query string中获得值。redirect (从django.shortcuts)以所需的值作为参数重定向到视图。我前面提到的文档中记录了如何检查POST或GET的方式。
https://stackoverflow.com/questions/24135975
复制相似问题