Reputation: 362
i created a view to delete an object through a template but it seems im missing something i cant quite put together
def product_delete_view(request, id):
obj = get_object_or_404(Product, id=id)
if request.method == "POST":
obj.delete()
return redirect('../../')
context = {
"obj": obj
}
return render(request, "products/product_delete.html", context)
from django.contrib import admin
from django.urls import path
from pages.views import home_view, contact_view, about_view, social_view, services_view
#from products.views import product_detail_view, product_create_view, render_intial_data
from products.views import render_intial_data, dynamic_lookup_view, product_delete_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='home'),
path('contact/', contact_view, name='contact'),
path('about/', about_view, name='about'),
path('services/', services_view, name='services'),
path('social/', social_view, name='social'),
#path('product/detail', product_detail_view, name='pdtDetail'),
#path('product/create', product_create_view, name='pdtCreate')
path('product/create', render_intial_data, name='pdtCreate'),
path('product/<int:id>', dynamic_lookup_view, name="pdt"),
path('product/<int:id>/delete', product_delete_view, name='pdtDelete')
]
{% extends 'base.html' %} {% block content %}
<form action="." method="POST">
{% csrf_token %}
<h1>Do you want to delete the product "{{ obj.title }}"</h1>
<p><input type="submit" value="Yes" /> <a href="../">Cancel</a></p>
</form>
{% endblock %}
when i try to delete object is not removed from the database table
Upvotes: 1
Views: 1058
Reputation:
your problem seems here
path('product/<int:id>/delete', product_delete_view, name='pdtDelete')
you had left that url without closing with /
. just do this
path('product/<int:id>/delete/', product_delete_view, name='pdtDelete')
Upvotes: 1
Reputation: 332
In your urls.py
there is something like this product/<int:id>/delete
and you are typing produck/4/
in your browser. use this http://127.0.0.1:8000/product/4/delete
and use '/
' at last like this http://127.0.0.1:8000/product/4/delete/
Upvotes: 1
Reputation: 47364
Your patterns doesn't ending with slash. Try to add ending slash to it:
path('product/create/', render_intial_data, name='pdtCreate'),
path('product/<int:id>/', dynamic_lookup_view, name="pdt"),
path('product/<int:id>/delete/', product_delete_view, name='pdtDelete')
Upvotes: 2