user1881957
user1881957

Reputation: 3388

request.POST.getlist returning None list in Django?

I have a html template like this:

<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 is generated from:

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

This is where I try to get the value from choice from template:

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

Upvotes: 2

Views: 7262

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599788

As Rohan mentioned in the comment, you have no <form> tag in the template, and appear to be simply assuming that clicking on a normal <a> link will submit some data. That's not at all how it works: if you want to submit data from input elements, they need to be inside a <form>, you need to set the action of that form appropriately, and you need to submit it using a <input type="submit"> (or button) rather than a link.

This is basic HTML, by the way, not Django.

Upvotes: 3

kartheek
kartheek

Reputation: 6694

Include [] after choice because you are getting array form request

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

this will solve your prob

Upvotes: 5

Related Questions