我是网页设计和HTML的新手,我需要一个ModelChoiceField,其中填充了来自零件数据库的唯一值(特别是'CATEGORY'字段)。经过一番挖掘后,我相信我已经想出了怎么做(虽然我可能错了),但现在我需要知道如何在我的index.html上显示它。这就是我所拥有的:
Forms.py:
class dropdown(forms.Form):
cats = forms.ModelChoiceField(queryset=Part.objects.distinct('CATEGORY'))
class Meta:
model = Part
Views.py:
#attempt at a dropdown form
def show_part(request):
form = dropdown()
if request.method == "POST":
form = dropdown(request.POST)
if form.is_valid:
#redirect to the url where you'll process the input
return HttpResponseRedirect('/templates/index.html') # insert reverse or url
errors = form.errors or None # form not submitted or it has errors
return render(request, '/collection/templates/index.html',{
'form': form,
'queryset': queryset,
'errors': errors,
})
index.html(我的模板)
<form action="cat-select1/" method="POST">
{% csrf_token %}
<label for="cat_select1"> Part Category: </label>
{{ form.cats }}
<input type="submit" value="Submit" />
</form>
为什么我的下拉列表显示在页面上?提前谢谢。