在我的模板choose_values.html
中,我有一个下拉列表,我选择了一些值。通过将request.POST分配给变量,我可以将这些值作为QueryDict传递给视图tomatos
。
我想在视图tomatos
我不知道如何从此QueryDict中提取所选值。
这是示例QueryDict:
<QueryDict: {u'csrfmiddlewaretoken': [u'pFs9fj5MLQML7KLYHXFeCWTSvuMFEoML'], u'dropdown1': [u'project1'], u'dropdown2': [u'S1'], u'dropdown3': [u'S1.2']}>
我该怎么办?
choose_values.html
CHOOSE_VALUES
<form method="post" action="{% url 'projects:manage_values' %}">{% csrf_token %}
<select name="dropdown1">
<option value="project1">project1</option>
<option value="project1">project2</option>
</select>
<select name="dropdown2">
<option value="S1">S1</option>
<option value="S2">S2</option>
<option value="all">all</option>
</select>
<select name="dropdown3">
<option value="S1.1">S1.1</option>
<option value="S1.2">S1.2</option>
<option value="all">all</option>
</select>
<input type="submit" value="submit" />
</form>
tomatos view
def tomatos(request):
context = RequestContext(request)
ZoneSubFormSet = modelformset_factory(ZoneSubStage, fields=('zone', 'substage','value'),max_num=2)
dupa=request.POST
print dupa
if request.method == 'POST':
formset = ZoneSubFormSet(queryset=ZoneSubStage.objects.filter(substage__stage__stage_name='S1',
substage__stage__project__name='project1',
zone__zone_name='z1'))
else:
formset = ZoneSubFormSet(queryset=ZoneSubStage.objects.filter(substage__stage__stage_name='S2',
substage__stage__project__name='project1',
zone__zone_name='z2'))
return render_to_response('autostages/update_values.html', {'formset': formset}, context)
修改
由于某些原因我不更新值,我不知道为什么。
def tomatos(request):
context = RequestContext(request)
ZoneSubFormSet = modelformset_factory(ZoneSubStage, fields=('zone', 'substage','value'),max_num=2)
value1=str(request.POST.get('dropdown1'))
value2=str(request.POST.get('dropdown2'))
value3=str(request.POST.get('dropdown3'))
if request.method == 'POST':
formset = ZoneSubFormSet(queryset=ZoneSubStage.objects.filter(substage__stage__project__name=value1,
substage__stage__stage_name=value2,
zone__zone_name=value3))
if formset.is_valid():
formset.save(commit=True)
else:
formset = ZoneSubFormSet(queryset=ZoneSubStage.objects.filter(substage__stage__project__name=value1,
substage__stage__stage_name=value2,
zone__zone_name=value3))
return render_to_response('autostages/update_values.html', {'formset': formset}, context)
答案 0 :(得分:1)
您可以将此QueryDict作为普通字典使用,在这种情况下,例如,如果您想获得dropdown1
值:
request.POST.get('dropdown1')