Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting after AJAX post in Django

I use Django's built-in DeleteView and I've assigned a value to the success_url attribute. Now in my template, I trigger this view via JQuery' $.post() method. When the item is deleted, I don't get redirected to the success_url. After some search, I found that it seems to be a problem of AJAX post method, which ignores the redirection.

I fixed it by adding a function to set the window.location="#myRedirectionURL" as the third parameter of $.post() in JQuery.

However, this way seems not very Django. Essentially, it solves the problem from the aspect of AJAX, instead of Django. What's more, it leaves the success_url in DeleteView useless( But you still have to assign a value to success_url, or Django will raise an error).

So is it the right way to get redirected when you post via AJAX? Is there any better way to do it?

Thanks very much.

like image 854
Xiao Liang Avatar asked Mar 19 '15 05:03

Xiao Liang


People also ask

How to redirect a URL in Django?

To avoid hard-coding the URL, you can call redirect () with the name of a view or URL pattern or a model to avoid hard-coding the redirect URL. You can also create a permanent redirect by passing the keyword argument permanent=True. This article could end here, but then it could hardly be called “The Ultimate Guide to Django Redirects.”

How to carry out request with jQuery Ajax in Django?

Carry out request with Jquery AJax. Register models to admin and add some posts. 1. Initiate the Django Project – Here I am assuming that you are done with Django Installation. After creating a project we need to create a django app. To create an app say “post” execute the following:

What is Ajax in Django and how to use it?

In simpler words, AJAX allows web pages to be updated asynchronously by exchanging data with a web server behind the scenes. This means that updating parts of a web page is possible, without reloading the whole page. We can make AJAX requests from Django templates using JQuery.

What is an unvalidated redirect in Django?

This kind of redirect is called an open or unvalidated redirect. There are legitimate use cases for redirecting to URL that is read from user input. A prime example is Django’s login view. It accepts a URL parameter next that contains the URL of the page the user is redirected to after login.


Video Answer


1 Answers

Ajax will not redirect pages!

What you get from a redirect is the html code from the new page inside the data object on the POST response.

If you know where to redirect the user if whatever action fails, you can simply do something like this:

On the server,

In case you have an error

response = {'status': 0, 'message': _("Your error")}  

If everything went ok

response = {'status': 1, 'message': _("Ok")} # for ok 

Send the response:

return HttpResponse(json.dumps(response), content_type='application/json') 

on the html page:

$.post( "{% url 'your_url' %}",           { csrfmiddlewaretoken: '{{ csrf_token}}' ,             other_params: JSON.stringify(whatever)          },            function(data) {              if(data.status == 1){ // meaning that everyhting went ok                 // do something              }              else{                 alert(data.message)                 // do your redirect                 window.location('your_url')              }         }); 

If you don't know where to send the user, and you prefer to get that url from the server, just send that as a parameter:

response = {'status': 0, 'message': _("Your error"), 'url':'your_url'}  

then substitute that on window location:

 alert(data.message)  // do your redirect  window.location = data.url; 
like image 89
brunofitas Avatar answered Sep 29 '22 05:09

brunofitas