John Allie
John Allie

Reputation: 255

Deleting object from a view in Django

I have a page that shows a list of objects and I want to add a button beside each one to enable the user to delete it. I've found a few questions about similar scenarios online but for some reason I keep getting errors when I try to replicate the solutions.

Here is the delete function in views.py:

def delete_dish(request, pk):
    query = Dish.objects.get_object_or_404(pk)
    supermenu = query['supermenu'].pk
    query.delete()
    return redirect('foodmenu:menu', supermenu)

Here is the form in the HTML template:

            {% if not supermenu.dish_set.count == 0 %}
            <ul>
            {% for dish in supermenu.dish_set.all %}


                <li>
                {{ dish.name }} - {{ dish.description }}
                <form action="{%  url 'foodmenu:delete_dish' dish.id %}" method="POST">
                    {% csrf_token %}
                    <button type="submit">X</button>
                </form>

                </li>


                    {% if not dish.price_set.count == 0 %}
                    <ul>
                    {% for price in dish.price_set.all %}
                        {% if price.label %}
                            <li>{{ price.label }}: {{ price.cost }}</li>
                        {% else %}
                            <li>{{ price.cost }}</li>
                        {% endif %}
                    {% endfor %}
                    </ul>
                    {% endif %}


                {% endfor %}
            </ul>

            {% else %}
            <p>No dishes on this menu!</p>

        {% endif %}

And here is the urls.py:

app_name = 'foodmenu'
urlpatterns = [
    ...
    path('dish/delete/<int:dish.id>', views.delete_dish, name="delete_dish")
]

When I click the button, the browser goes to ./dish/delete/1 (1 being the pk of the object), but Django returns a 404 error.

Upvotes: 1

Views: 1035

Answers (1)

neverwalkaloner
neverwalkaloner

Reputation: 47354

Instead of query = Dish.objects.get_object_or_404(pk) you should use get_object_or_404 shortcut:

from django.shortcuts import get_object_or_404

from django.views.decorators.http import require_POST

@require_POST
def delete_dish(request, pk):
    if request.method
    query = get_object_or_404(Dish, pk=pk)
    supermenu = query.supermenu.pk
    query.delete()
    return redirect('foodmenu:menu', supermenu)

Also change your url pattern to this:

path('dish/delete/<int:pk>/', views.delete_dish, name="delete_dish")

UPD

As @daniherrera mentioned in his comment, you probably want to check request's method, to prevent accidental deletions. For this you can use require_POST decorator.

Upvotes: 2

Related Questions