我的应用程序中有两个独立的视图,我尝试使用HttpResponseRedirect从另一个视图调用一个视图,但它根本不起作用。它仍然在同一页面,没有重定向。任何人都可以帮我找出导致错误的原因吗?
在我的view.py
中def viewStore(request):
if request.POST:
if request.POST.get('delete_selected'):
id = int(request.POST.get('check'))
HttpResponseRedirect('/dataInfo/store_view/delete/%d/' %id)
view_store = Store.objects.all()
context = {'view_store': view_store}
return render(request, 'dataInfo/store_view.html', context)
def deleteStore(request):
if request.POST.get('delete_selected'):
Store.objects.filter(pk__in=request.POST.get('check')).delete()
HttpResponseRedirect('/dataInfo/store_view/')
在我的urls.py中
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^store_view/$', views.viewStore, name='viewStore'),
url(r'^add_store/$', views.addStore, name='addStore'),
url(r'^thanks/$', views.thanks, name='thanks'),
url(r'^store_view/delete/(\d+)/$', views.deleteStore, name='deleteStore'),
]
答案 0 :(得分:1)
问题是,当您的浏览器发送POST请求并收到重定向时,它将遵循带有GET请求的重定向。
viewStore
。viewStore
会将您重定向到deleteStore
视图deleteStore
视图deleteStore
未删除,因为它是GET请求,然后将您重定向到viewStore
您应该更改模板中的表单,以便直接发布到deleteStore
视图。
答案 1 :(得分:0)
您永远不会返回回复,只需创建回复即可。只需return
回复。
def viewStore(request):
if request.POST:
if request.POST.get('delete_selected'):
id = int(request.POST.get('check'))
return HttpResponseRedirect('/dataInfo/store_view/delete/%d/' %id)
view_store = Store.objects.all()
context = {'view_store': view_store}
return render(request, 'dataInfo/store_view.html', context)
def deleteStore(request):
if request.POST.get('delete_selected'):
Store.objects.filter(pk__in=request.POST.get('check')).delete()
return HttpResponseRedirect('/dataInfo/store_view/')
答案 2 :(得分:0)
您必须返回 HttpResponseRedirect
。那是你的错误