Checkboxes for a list of items like in Django admin interface

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Checkboxes for a list of items like in Django admin interface

Martin Tiršel-3
Hello,

I have a list of items (from a model) I want to display with checkboxes  
(for deleting multiple objects at once). The same behaviour like in django  
admin (change list). I can do such thing in templates:

{% for item in item_list %}
<li>
<input type="checkbox" name="somename" value="item_{{ item.id }}">
{{ item }}
</li>
{% endfor %}

and in a view loop through somename list I get from POST and delete items.  
Is there a better Django way I should do it or this approach I wrote is  
correct? Is there a way I could use Django forms for processing the POST  
data and delete items in a form's method (so I can use it from multiple  
views)?

Thanks,
Martin

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

Reply | Threaded
Open this post in threaded view
|

Re: Checkboxes for a list of items like in Django admin interface

kenneth gonsalves
On Wed, 2012-01-04 at 11:33 +0100, Martin Tiršel wrote:
> and in a view loop through somename list I get from POST and delete
> items.  
> Is there a better Django way I should do it or this approach I wrote
> is  
> correct?

I have been doing the same - would love to know if there was a better
way.
--
regards
Kenneth Gonsalves

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

Reply | Threaded
Open this post in threaded view
|

Re: Checkboxes for a list of items like in Django admin interface

Thorsten Sanders
In reply to this post by Martin Tiršel-3
I do it this way:

<input type="checkbox" name="todelete" value="{{ item.id }}"/>

and in the view:

todel = request.POST.getlist('todelete')
ItemWatchList.objects.filter(user=request.user,id__in=todel).delete()


On 04.01.2012 11:33, Martin Tiršel wrote:

> Hello,
>
> I have a list of items (from a model) I want to display with
> checkboxes (for deleting multiple objects at once). The same behaviour
> like in django admin (change list). I can do such thing in templates:
>
> {% for item in item_list %}
> <li>
> <input type="checkbox" name="somename" value="item_{{ item.id }}">
> {{ item }}
> </li>
> {% endfor %}
>
> and in a view loop through somename list I get from POST and delete
> items. Is there a better Django way I should do it or this approach I
> wrote is correct? Is there a way I could use Django forms for
> processing the POST data and delete items in a form's method (so I can
> use it from multiple views)?
>
> Thanks,
> Martin
>

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

Reply | Threaded
Open this post in threaded view
|

Re: Checkboxes for a list of items like in Django admin interface

Martin Tiršel-3
Yes, this is the technique I described but I was asking if there is a  
Django way to do this.

You can't forgot to sanitize the input in this case:

id_list = request.GET.getlist('id_notification_list')
id_list = [int(i) for i in id_list if i.isdigit()]
notifications = Notification.objects.filter(
     id__in=id_list,
)

Martin

On Wed, 04 Jan 2012 12:11:11 +0100, Thorsten Sanders  
<[hidden email]> wrote:

> I do it this way:
>
> <input type="checkbox" name="todelete" value="{{ item.id }}"/>
>
> and in the view:
>
> todel = request.POST.getlist('todelete')
> ItemWatchList.objects.filter(user=request.user,id__in=todel).delete()
>
>
> On 04.01.2012 11:33, Martin Tiršel wrote:
>> Hello,
>>
>> I have a list of items (from a model) I want to display with checkboxes  
>> (for deleting multiple objects at once). The same behaviour like in  
>> django admin (change list). I can do such thing in templates:
>>
>> {% for item in item_list %}
>> <li>
>> <input type="checkbox" name="somename" value="item_{{ item.id }}">
>> {{ item }}
>> </li>
>> {% endfor %}
>>
>> and in a view loop through somename list I get from POST and delete  
>> items. Is there a better Django way I should do it or this approach I  
>> wrote is correct? Is there a way I could use Django forms for  
>> processing the POST data and delete items in a form's method (so I can  
>> use it from multiple views)?
>>
>> Thanks,
>> Martin
>>

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

Reply | Threaded
Open this post in threaded view
|

Re: Checkboxes for a list of items like in Django admin interface

Thorsten Sanders
I missunderstood due "and in a view loop through somename list" thought
you mean you delete them 1 by 1 :P

And the orm itself tries to convert the value to an int and throws an
ValueError if it fails, so find it a bit double to check myself and let
the orm do it again.

On 04.01.2012 13:43, Martin Tiršel wrote:

> Yes, this is the technique I described but I was asking if there is a
> Django way to do this.
>
> You can't forgot to sanitize the input in this case:
>
> id_list = request.GET.getlist('id_notification_list')
> id_list = [int(i) for i in id_list if i.isdigit()]
> notifications = Notification.objects.filter(
>     id__in=id_list,
> )
>
> Martin
>
> On Wed, 04 Jan 2012 12:11:11 +0100, Thorsten Sanders
> <[hidden email]> wrote:
>
>> I do it this way:
>>
>> <input type="checkbox" name="todelete" value="{{ item.id }}"/>
>>
>> and in the view:
>>
>> todel = request.POST.getlist('todelete')
>> ItemWatchList.objects.filter(user=request.user,id__in=todel).delete()
>>
>>
>> On 04.01.2012 11:33, Martin Tiršel wrote:
>>> Hello,
>>>
>>> I have a list of items (from a model) I want to display with
>>> checkboxes (for deleting multiple objects at once). The same
>>> behaviour like in django admin (change list). I can do such thing in
>>> templates:
>>>
>>> {% for item in item_list %}
>>> <li>
>>> <input type="checkbox" name="somename" value="item_{{ item.id }}">
>>> {{ item }}
>>> </li>
>>> {% endfor %}
>>>
>>> and in a view loop through somename list I get from POST and delete
>>> items. Is there a better Django way I should do it or this approach
>>> I wrote is correct? Is there a way I could use Django forms for
>>> processing the POST data and delete items in a form's method (so I
>>> can use it from multiple views)?
>>>
>>> Thanks,
>>> Martin
>>>
>

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

Reply | Threaded
Open this post in threaded view
|

Re: Checkboxes for a list of items like in Django admin interface

DrBloodmoney
In reply to this post by Martin Tiršel-3
On Wed, Jan 4, 2012 at 5:33 AM, Martin Tiršel <[hidden email]> wrote:

> Hello,
>
> I have a list of items (from a model) I want to display with checkboxes (for
> deleting multiple objects at once). The same behaviour like in django admin
> (change list). I can do such thing in templates:
>
> {% for item in item_list %}
> <li>
> <input type="checkbox" name="somename" value="item_{{ item.id }}">
> {{ item }}
> </li>
> {% endfor %}
>
> and in a view loop through somename list I get from POST and delete items.
> Is there a better Django way I should do it or this approach I wrote is
> correct? Is there a way I could use Django forms for processing the POST
> data and delete items in a form's method (so I can use it from multiple
> views)?
>

I do it in a form, because I like to keep all of the heavy logic out
of views and keep it in the models or the forms. Here is a relatively
complete example form from my codebase:

class DivisionRemovalForm(forms.Form):
    class MultipleDivisionField(forms.ModelMultipleChoiceField):
        def label_from_instance(self, obj):
            url = reverse('organizer_division_view', kwargs={'div_pk': obj.pk})
            label = '<a href="%s">%s</a>' % (url, obj.__unicode__())
            return mark_safe(label)

    divisions = MultipleDivisionField(queryset=[],
                widget=forms.CheckboxSelectMultiple())

    def __init__(self, competition, *args, **kwargs):
        self.competition = competition
        super(DivisionRemovalForm, self).__init__(*args, **kwargs)
        self.fields['divisions'].queryset = self.competition.divisions.all()

    def save(self):
        for div in self.cleaned_data['divisions']:
            units = div.units.all()
            if units:
                for unit in units:
                    unit.in_division = False
                    unit.save()
        divisions = self.cleaned_data['divisions']
        divisions.delete()

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