prog.Dusan
prog.Dusan

Reputation: 1424

Django: Delete checked items using generic DeleteView?

I have a table where I listing all my items with two columns(delete,URL name). In delete column there are check boxes for each item, and one check box for select all items.

How can I implement it using generic view DeleteView in Django? Is it possible?

My example:

models.py

class UrlStatus_Proxy(models.Model):

    urls = models.URLField(u'Site URL', max_length=100, null=True, unique=True)
    status_url = models.CharField(u'Site', max_length=20, choices=STATUS_URL)

    def __unicode__(self):
        return self.urls 

views.py class ProxyUrlListView(ListView):

    model = UrlStatus_Proxy

    def get_context_data(self, **kwargs):
        context = super(ProxyUrlListView,
                        self).get_context_data(**kwargs)

        context['allow_urls'] = UrlStatus_Proxy.objects.filter(status_url__startswith='allow')
        context['block_urls'] = UrlStatus_Proxy.objects.filter(status_url__startswith='block')
        return context

urls.py

url(r'^url_status/(?P<pk>\d+)/delete/?$',ProxyUrlDeleteView.as_view(),name='delete_proxy'),
url(r'url_status/$', ProxyUrlListView.as_view(template_name='example.html'))

I've wrote template just for block_urls objects in this moment, cuz It is enough to figure out problem.

example.html

<form action="" method="post" enctype="multipart/form-data"> {% csrf_token %} {{ form.errors }}
        {% if block_urls %}
                <li>
                    {% csrf_token %}
                    <div class="hastable">
                        <table>
                            <thead>
                            <tr>
                                <th>{% trans 'Site Url' %}</th>
                                <th><label>{% trans 'Delete' %}</label><input type="checkbox" value="check_one" onclick="check(this)" class="submit"/></th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for block in block_urls %}
                                <tr>
                                    <td style="text-align: center">{{ block.urls }}</td>
                                    <td style="text-align: center"><input type="checkbox" value="{{ block.id }}" name="list" class="checkbox"/></td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    <div style="text-align: right">
                        <input type="submit" id="delete_selected_block" name="delete_selected_block " value="Delet selected" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">

                    </div>
                </li>
        {% endif %}
</form>

How can I solve this problem using DeleteView that I displayed above in urls.py

Upvotes: 1

Views: 1197

Answers (1)

frague
frague

Reputation: 209

DeleteView is a SingleObjectMixin subclass, so it can only be used to deleta a sngle object.

You should :

  • use ajax based deletions

or

  • create a BundledDeleteListView, based on BaseListView, to perform the bundled deletions.

Good coding !

Upvotes: 1

Related Questions