user12207810
user12207810

Reputation:

Why is my delete button not deleting my database object? Django

I cobbled together a delete button and edited my view, it isn't working. Can someone help me fix it?

I've moved some code around and tried some things but I can't get it to work. I need someone to show me what I'm doing wrong.

My view:

def post_edit(request, pk):
    post = get_object_or_404(Listing, pk=pk)
    if request.method == "POST":
        form = ListingForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            return redirect('post_view', pk=post.pk)
    else:
        form = ListingForm(instance=post)
    if request.POST.get('delete'):
        post.delete()
        return redirect('listings')
    return render(request, 'post_edit.html', {'form': form})

My html:

{% extends 'base.html' %}

{% block title %}Post Edit{% endblock %}

{% block content %}
    Hi {{ user.username }}!
    <p><a href="{% url 'logout' %}">logout</a></p>

    <h1>Edit listing:</h1>
    <p>The listing will only be viewable to users if "Is Live" is checked.</p>
    <form method="POST" enctype="multipart/form-data" class="post-form">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Save</button>
        <p>Click the button below to delete this listing. No second warning is given, once you click delete it will be
            removed.</p>
        <button type="delete" class="delete btn btn-default">delete</button>
    </form>

{% endblock %}

Upvotes: 1

Views: 843

Answers (1)

Nico Griffioen
Nico Griffioen

Reputation: 5405

"delete" is not a valid type for an HTML form control. You'l need to change it to "submit" (Since you still want to submit the form).

What you'll want to do is create two buttons with the same name, and different values like this:

<button type="submit" name="submit" value="submit" class="save btn btn-default">Save</button>
<p>Click the button below to delete this listing. No second warning is given, once you click delete it will be
            removed.</p>
<button type="submit" name="submit" value="delete" class="delete btn btn-default">delete</button>

Then you can check in your view if the delete button was clicked, like this:

def post_edit(request, pk):
    post = get_object_or_404(Listing, pk=pk)
    if request.method == "POST":
        if request.POST.get('submit') == 'delete':
             post.delete()
             return redirect('listings')
        form = ListingForm(request.POST, instance=post)
        if form.is_valid():
            post = form.save(commit=False)
            post.save()
            return redirect('post_view', pk=post.pk)
    else:
        form = ListingForm(instance=post)
    return render(request, 'post_edit.html', {'form': form})

Note that I move the check for the delete button to inside the if request.method == "POST": block, for two reasons:

  1. You'll only want to check the POST values if it's actually a post method.

  2. There is no use editing a post, then deleting it.

Upvotes: 1

Related Questions