Friday, February 4, 2011

Re: No POST response when using checkbox form

aahhh yes!!! Thank you very much!!!  Alright, ill work on implementing those form views :)

On Fri, Feb 4, 2011 at 4:25 AM, Daniel Roseman <daniel@roseman.org.uk> wrote:
On Friday, February 4, 2011 9:24:48 AM UTC, Daniel Roseman wrote:
On Friday, February 4, 2011 12:35:20 AM UTC, Ethan Yandow wrote:
Hey there, I am trying to delete Events as chosen by a by a user using
check boxes to check of which events they want to be deleted.  But for
some reason whenever I call request.POST.get('event_list') Nothing is
received even though boxes are checked and I end up with nothing.
Here is my template and the view that should be deleting the chosen
events.

 {% if event_list %}
{% for event in event_list%}
{%csrf_token%}
<input type="checkbox" name="event_list"
id="event{{ forloop.counter }}" />
<label for="event{{ forloop.counter }}">{{ event.title }}</
label><br />
{% endfor %}
<input type = 'submit' value = 'delete checked'>
</form>
<p>{{removal}}<p/>    {%comment%} this is what should be
removed{%endcomment%}
{% if delete_error %}
<p>{{delete_error}}</p>
{% endif %}

views.py

def EventDelete(request):
removal = request.POST.get('event_list')
if removal:
removal.delete()
        else:
delete_error = "You didn't delete anything"
return redner_to_response("detail.html", {'delete_error':
delete_error, 'removal': removal},
context_instance=RequestContext(request))

Im not sure why removal doesn't have anything in it, shouldn't it have
the titles of the events in it?

You haven't defined a `value` attribute for each checkbox. 

<input type="checkbox" name="event_list" value="{{ event.id }}" id="event{{ forloop.counter }}" /> 

Also, in your view, you should use `getlist` to get the value - `get` only gets the first element if it's a multi-valued field.
--
DR.

Meant to add, you should really be using Django's forms framework, as it takes care of all of this for you.
--
DR.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment