2015-11-04 57 views
1

在我的模板choose_values.html我有一個下拉列表,我選擇一些值。通過將request.POST分配給變量,我可以將這些值作爲QueryDict傳遞給視圖tomatosdjango方法POST和Querydict

我想用選擇值過濾器鑑於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> 

番茄查看

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) 

回答

1

你可以用這個的QueryDict作爲普通字典的工作,在這種情況下,例如,如果你想獲得dropdown1值:

request.POST.get('dropdown1') 
+0

喜,因爲某些原因,我沒有更新我的日期。你能幫忙嗎?我已經更新了我的帖子中的代碼,我可以看到我不執行formset.save() – BlueTomato