request.POST.getlist是否在Django中返回无列表?

request.POST.getlist是否在Django中返回无列表?,django,Django,我有这样一个html模板: <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{{ fo

我有这样一个html模板:

<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>
这是我尝试从模板中选择获取值的地方:

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))

在选择后包含[],因为您正在获取数组表单请求

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


这将解决您的问题

正如Rohan在评论中提到的,您在模板中没有
标记,并且似乎只是假设单击正常的
链接将提交一些数据。这根本不是它的工作原理:如果您想从输入元素提交数据,它们需要在
中,您需要适当地设置该表单的
操作
,并且您需要使用
(或
按钮
)而不是链接来提交数据


顺便说一句,这是基本的HTML,不是Django。

模板中没有
form
标记。不,它没有。我仍然得到空列表。如果要传递该视图的post数据,则需要传递数据表单模板。使用查询字符串传递数据请仔细阅读我的问题。我已经传递了上传的_文件中的数据,我正在访问delete_文件中的数据。现在你明白我的意思了,对于delete,共享你可以使用Ajax,jQuery我更喜欢这个。它很容易使用:)但是丹尼尔,如果我想要像删除、共享这样的单独按钮怎么办!对于“删除”,我想处理“删除”文件,但对于“共享”,我想处理“共享”文件。我该怎么做?我只能在表单中指定一个查看操作。您可以有多个提交按钮,相关按钮的值将包含在发布数据中。您可以在视图中使用该值分派到正确的函数。您的意思是,如果我输入:,我也可以访问共享文件视图中的POST值吗?否。我的意思是您的操作应该指向一个组合分派函数,但您有两个提交按钮,一个带有
name='delete'
,另一个带有
name='share'
,然后在视图中检查这些元素中的哪一个在request.POST中,并调用相关函数。通常建议首先检查request dict中的字段名。但是如果我使用as doc说
“它保证返回某种类型的列表,除非默认值是no list。”
所以对于select multiple,我不检查字段名,我只检查
L=request.POST.getlist('fieldname')
,然后使用list
L
。这样做有什么害处吗?我说得对吗?
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))