user2232982
user2232982

Reputation: 1395

Delete items from ListView in Django 1.5

I have a ListView and a DeleteView

class MyDeleteView(DeleteView):
    success_url = reverse('list')

I want the option to delete the items in the ListView. I know how to do it if I accept the confirmation page in the DeleteView, but I don't want no template in my DeleteView. I just want to delete the item and send the user back.

I guess it should be with POST parameters, but what should the HTML look like? I guess it's something like:

<form method="post" action="/delete/">
    <ul>
        <li>Item1 (<input type="submit" value="Delete" />)</li>
        <li>Item2 (<input type="submit" value="Delete" />)</li>
        <li>Item3 (<input type="submit" value="Delete" />)</li>
    </ul>
</form>

Can anyone lead me in the right direction? Thank you.

Upvotes: 6

Views: 2876

Answers (2)

Chris Wesseling
Chris Wesseling

Reputation: 6368

You're already heading the right way, with POST.

<ul>{% for item in object_list %}        
    <li><form method="post" action="{% url 'mydelete' pk=item.pk %}">
          {{item}} (<input type="submit" value="Delete" />)
    </form></li>
{% endif %}</ul>

I'm not entirely sure if the the inputs can go directly in a form in the HTML spec you're trying to adhere to. So you might have to sprinkle this idea with some spans or containers.

If the input submit, doesn't give your designers enough styling freedom, you could use them as the <noscript> fallback and add some <button> or javascript: link for the pretty version.

Upvotes: 3

Pavel Anossov
Pavel Anossov

Reputation: 62938

Since you don't want a confirmation, you can override the GET method in your deleteview and just use links:

class MyDeleteView(DeleteView):
    success_url = reverse('list')

    def get(self, *a, **kw):
        return self.delete(*a, **kw)

<ul>
    {% for item in object_list %}
        <li>Item1 (<a href="{% url 'mydelete' pk=item.pk %}">Delete</a>)</li>
    {% endif %}
</ul>

Upvotes: 0

Related Questions