2013-01-15 155 views
2

我有一個HTML模板是這樣的:request.POST.getlist在Django中返回無列表?

def uploaded_files(request): 
    log_id = request.user.id 
    b = File.objects.filter(users_id=log_id, flag='F') #Get the user id from session .delete() to use delete 
    return render_to_response('upload.html', {'result': b}, context_instance=RequestContext(request)) 

這是我嘗試從模板從選擇獲得的價值:

def delete_files(request): 
    log_id = request.user.id 
    choices = request.POST.getlist('choice') #Get the file name from the as a list 
    for i in choices: 
     File.objects.filter(users_id=log_id, file_name=i).update(flag='D') 
    return render_to_response('upload.html', {'c': choices}, context_instance=RequestContext(request)) 
+2

模板中沒有'form'標籤。 – Rohan

回答

3

由於羅漢在評論中提到,你必須在模板中沒有<form>標記,似乎只是假設一下正常<a>鏈接將提交一些數據。這根本不是它的工作方式:如果你想從輸入元素提交數據,他們需要在<form>內,你需要適當地設置該表格的action,並且你需要使用<input type="submit">(或button)提交它,而不是一個鏈接。

這是基本的HTML,順便說一下,不是Django。

+0

但丹尼爾如果我想單獨的按鈕,如刪除,共享!對於刪除,我想處理delete_files,但對於共享我想處理share_files。我怎樣才能做到這一點?我只能在表單中指定1個視圖操作。 – user1881957

+1

您可以有多個提交按鈕,相關的值將包含在POST數據中。您可以在視圖中使用該值來分派到正確的功能。 –

+0

你的意思是如果我把:

,我也可以在shared_files視圖中訪問POST值嗎? – user1881957

4

<table border="1" align="center"> 

{% for result in result %} 
<tr> 
<td><input type="checkbox" name="choice" id="choice{{ forloop.counter }}" value="{{ choice }}" /></td> 
<td><label for="choice{{ forloop.counter }}">{{ choice }}</label><br /></td> 
<td>{{ result.file_name }}</td> 
<td>{{ result.type }}</td> 
<td>{{ result.size }}</td> 
<td>{{ result.end_date }}</td> 
<td>{{ result.source }}</td> 
{% endfor %} 
</tr> 
</table> 
{{ c }} 
<h4><a href="/delete_files/">Delete File</a></h4> 

result variable從產生包括[]後選擇,因爲您正在獲取陣列形式的請求

choices = request.POST.getlist('choice[]')

這將解決您的概率

+0

不,它不。我仍然得到空單。 – user1881957

+0

您在哪裏傳遞發佈數據 – kartheek

+0

在delete_files視圖中。 – user1881957