我试图在删除一个我不知道但无法正常工作的对象后重定向到页面。该对象将被删除,但不会重定向到任何页面,有人可以帮助我吗? 如果您需要更多代码,可以询问我。 谢谢 !
删除产品的视图
@login_required()
def delete_product(request, product_id):
delete = get_object_or_404(Product, pk=product_id)
form = NewPro(instance=delete)
if request.method == 'POST':
form = NewPro(request.POST, request.FILES, instance=delete)
if form.is_valid():
delete.delete()
return redirect('store_details')
else:
form = NewPro(instance=delete)
return render(request, "default/product_delete.html", {'form': form, 'delete': delete})
网址格式
from django.urls import path
from . import views
from stores.urls import views
app_name = 'stores'
urlpatterns = [
path('<int:store_id>/store_details/', views.store_details, name='store_details'),
]
urlpatterns = [
path('<int:product_id>/product', views.product, name='products_details'),
path('add_products/', views.pro, name='add_products'),
path('', views.index_products, name='index_products'),
path('<int:product_id>/detail_product', views.detail_product, name='detail_product'),
path('<int:product_id>/edit_product', views.edit_product, name='edit_product'),
path('<int:product_id>/delete_product', views.delete_product, name='delete_product')
]
答案 0 :(得分:1)
该错误表明django无法解析视图store_details
的路径。您可以使用reverse
中的django.urls
方法。
from django.urls import reverse
@login_required()
def delete_product(request, product_id):
delete = get_object_or_404(Product, pk=product_id)
form = NewPro(instance=delete)
if request.method == 'POST':
# if this form is not valid we rander it again, so no need for else statement
form = NewPro(request.POST, request.FILES, instance=delete)
if form.is_valid():
delete.delete()
return redirect(reverse('stores:store_details'))
# if it's not POST method render an empty form
else:
form = NewPro()
return render(request, "default/product_delete.html", {'form': form, 'delete': delete})
urls.py
产品应用:
from django.urls import path
from . import views
app_name = 'product'
urlpatterns = [
path('<int:product_id>/product', views.product, name='products_details'),
path('add_products/', views.pro, name='add_products'),
path('', views.index_products, name='index_products'),
path('<int:product_id>/detail_product', views.detail_product, name='detail_product'),
path('<int:product_id>/edit_product', views.edit_product, name='edit_product'),
path('<int:product_id>/delete_product', views.delete_product, name='delete_product')
]
urls.py
个商店应用:
from django.urls import path
from . import views
app_name = 'stores'
urlpatterns = [
path('<int:store_id>/store_details/', views.store_details, name='store_details'),
]
随附的URLconf模块中的 app_name
属性有助于反向解析URL
我希望这会有所帮助。
答案 1 :(得分:0)
您没有为发布请求设置表单,请将else:
移到选项卡上。
正确的语法:
@login_required()
def delete_product(request, product_id):
delete = get_object_or_404(Product, pk=product_id)
form = NewPro(instance=delete)
if request.method == 'POST':
form = NewPro(request.POST, request.FILES, instance=delete)
if form.is_valid():
delete.delete()
return redirect('store_details')
else:
form = NewPro(instance=delete)
return render(request, "default/product_delete.html", {'form': form, 'delete': delete})