Reputation: 725
There are two separated views in my app, and I try one calling one view from the other by using HttpResponseRedirect, but it does not work at all. It still in the same page and no redirection. Can anyone help me figure out where causes errors?
in my 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/')
In my 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'),
]
Upvotes: 1
Views: 207
Reputation: 308849
The problem is that when your browser sends a POST request and receives a redirect, it will follow the redirect with a GET request.
viewStore
.viewStore
redirects you to the deleteStore
viewdeleteStore
viewdeleteStore
does not delete because it's a GET request, then redirects you to the viewStore
You should change the form in your template so that it posts directly to the deleteStore
view.
Upvotes: 1
Reputation: 428
you must to return the HttpResponseRedirect
. That is your error
Upvotes: 1
Reputation: 43300
You never return the responses, you just create them. Simply return
the responses.
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/')
Upvotes: 0