Removing items from a list in Django 1.5

I have a ListView and DeleteView

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

I want the option to delete items in a ListView. I know how to do this if I accept a confirmation page in DeleteView, but I do not want my DeleteView to have no template. I just want to delete the item and send back the user.

I guess it should be with POST options, but what should the HTML look like? I think this is 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 someone lead me in the right direction? Thanks.

+6
source share
2 answers

Since you do not want confirmation, you can override the GET method in your deleteview and just use the 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> 
0
source

You are already heading right 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 quite sure that the input can go directly to the form in the HTML specification that you are trying to stick to. Therefore, you may have to sprinkle this idea with some spans or containers.

If the submit input file does not provide your designers with enough styling freedom, you can use them as a <noscript> and add some <button> or javascript: link for the beautiful version.

+2
source

All Articles