Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 590
user2232982 Avatar asked Apr 06 '13 23:04

user2232982


1 Answers

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.

like image 89
Chris Wesseling Avatar answered Sep 30 '22 14:09

Chris Wesseling