[英]dropdown that filters results in django
我想要django页面上的基本下拉框和一个提交按钮。 当用户从下拉菜单中选择某项并点击提交时,它将过滤结果。 这应该很容易,但是我已经花了几个小时,却找不到我想要做的事的例子。 如何从POST获取数据? 许多人建议不要使用原始POST数据,而应使用form.is_valid(),但是在这种情况下,我不使用form.py(不确定我需要使用form.py来使用form.is_valid吗?如果我可以使用forms.is_valid,如何提取用户选择的内容?)。
这是我的views.py
def dashboard(request):
plants = Plant.objects.all().order_by('IMS_plant')
if request.POST:
#selectedplant = #need to figure out how to get the value from the form in the template
sightings = Sighting.objects.all().filter(IMS_plant=selectedplant)
context = {'sightings':sightings, 'plants': plants}
else:
sightings = Sighting.objects.all().order_by('date')
context = {'sightings':sightings, 'plants': plants}
return render_to_response('dashboard.html', context, context_instance=RequestContext(request))
这是我的模板
<form action="/dashboard/" method="post"> {% csrf_token %} <select name="plant"> <option selected="selected">All Plants</option> {% for plant in plants %} <option value="{{ plant.IMS_plant }}">{{ plant.IMS_plant }}</option> {% endfor %} </select> <input type="submit" value="Select Plant"> </form> <p> {% for sighting in sightings %} <a href="/dashboard/sighting/{{ sighting.slug }}/ ">{{ sighting.date|date:"m/d/Y" }} {{ sighting.brand }} ${{ sighting.price|intcomma }} </a> <br>{% endfor %}
使用django-way。 使用表格。
forms.py:
class FilterForm(forms.Form):
selectedplant = forms.ModelChoiceField(queryset=Plant.objects.all().order_by('IMS_plant'), required=True)
view.py:
def dashboard(request):
form = FilterForm()
sightings = []
if request.POST:
form = FilterForm(request.POST)
if form.is_valid():
selectedplant = form.cleaned_data['selectedplant']
sightings = Sighting.objects.filter(IMS_plant=selectedplant)
else:
sightings = Sighting.objects.all().order_by('date')
else:
sightings = Sighting.objects.all().order_by('date')
context = {'sightings':sightings, 'form': form}
return render_to_response('dashboard.html', context, context_instance=RequestContext(request))
然后在模板中渲染{{form}}
我对EvilX的解决方案进行了一些调整,以便可以选择“全部显示”。
forms.py
class FilterForm(forms.Form):
selectedplant = forms.ModelChoiceField(queryset=Plant.objects.all().order_by('IMS_plant'), required=False, label='', empty_label="Show All")
views.py
def dashboard(request):
form = FilterForm()
sightings = []
if request.POST:
form = FilterForm(request.POST)
if form.is_valid():
selectedplant = form.cleaned_data['selectedplant']
if selectedplant == None:
sightings = Sighting.objects.all().order_by('date')
else:
sightings = Sighting.objects.filter(IMS_plant=selectedplant)
else:
sightings = Sighting.objects.all().order_by('date')
else:
sightings = Sighting.objects.all().order_by('date')
context = {'sightings': sightings, 'form': form}
return render_to_response('dashboard.html', context, context_instance=RequestContext(request))
这很好用,尽管我不为在sightings = Sighting.objects.all().order_by('date')
出现sightings = Sighting.objects.all().order_by('date')
3次这一事实感到高兴,但这并不是世界末日(只是希望看起来有点干净)。
再次感谢EvilX!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.