6

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.

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

3 Comments

What if I need to check if the user is the owner of the object? Is it something like def get(self, *args, **kwargs): self.object = self.get_object() if self.object.user == self.request.user: return self.delete(*args, **kwargs) else:
Yes, that would work. But you need to decide what to do if he's not (return HttpResponseForbidden?)
-1 GET should be safe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.