Reputation: 6204
I am using class based view with a get and a post methods
class MyView(View):
def get(self,request, *args, **kwargs):
#some code
def post(self,request, *args, **kwargs):
#update some data which is used in the view
return redirect('app:my-view')
the problem with this is that django does not re-execute the get
method after the redirect (the GET request can be confirmed in firebug). If I hit F5
manually in the browser, I can see the modified data.
This is probably a cache issue, but want to update the page but if the page is no reloaded in the GET after the POST, I can't do this.
Upvotes: 0
Views: 1227
Reputation: 6204
To solve this You have to redirect from the Ajax call:
$.post("",{active: status,
success: function(){
window.location = "";
},
'csrfmiddlewaretoken':jQuery("[name=csrfmiddlewaretoken]").val()});
This is done by the window.location="";
I am passing an empty string because I am redirecting to the same page, but you can pass any url.
Upvotes: 1
Reputation: 5492
Your redirect actually works, but as you are issuing the request with Ajax, your browser is not redirected, only your ajax request is being re-directed, and that won't change the page url in the browser.
That's actually the whole point of ajax requests, you exchange information with the server without changing the browser url. Anything you do with ajax requests have no effect on browser url.
If you post an html form instead, without ajax, to this url, your post method would be executed, and then your page would be redirected with a get request.
To do what you want with ajax, I suggest you do not redirect at the end of your post request, but return a success response. Then, on the client side, issue an ajax request, and reload the page on your request's success handler.
Upvotes: 1