在 Django 中的 AJAX 发布后重定向 [英] Redirecting after AJAX post in Django

查看:23
本文介绍了在 Django 中的 AJAX 发布后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Django 的内置 DeleteView,并为 success_url 属性分配了一个值.现在在我的模板中,我通过 JQuery 的 $.post() 方法触发这个视图.当项目被删除时,我不会被重定向到 success_url.经过一番搜索,发现好像是AJAX post方法的问题,忽略了重定向.

我通过添加一个函数来修复它,将 window.location="#myRedirectionURL" 设置为 JQuery 中 $.post() 的第三个参数.>

不过,这种方式好像不是很Django.本质上是从AJAX的角度解决问题,而不是Django.更重要的是,它使DeleteView 中的success_url 无用(但您仍然必须为success_url 赋值,否则Django 将引发错误).

那么当您通过 AJAX 发帖时,这是重定向的正确方式吗?有什么更好的方法吗?

非常感谢.

解决方案

Ajax 不会重定向页面!

您从重定向中获得的是 POST 响应数据对象内新页面的 html 代码.

如果您知道在任何操作失败时将用户重定向到哪里,您可以简单地执行以下操作:

在服务器上,

如果你有错误

response = {'status': 0, 'message': _("你的错误")}

如果一切顺利

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

发送响应:

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

在 html 页面上:

$.post( "{% url 'your_url' %}",{ csrfmiddlewaretoken: '{{ csrf_token}}' ,other_params: JSON.stringify(whatever)},功能(数据){if(data.status == 1){//表示一切正常//做一点事}别的{警报(数据.消息)//做你的重定向window.location('your_url')}});

如果您不知道将用户发送到哪里,并且您更喜欢从服务器获取该 url,只需将其作为参数发送:

response = {'status': 0, 'message': _("你的错误"), 'url':'your_url'}

然后在窗口位置替换它:

 alert(data.message)//做你的重定向window.location = data.url;

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.

解决方案

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;

这篇关于在 Django 中的 AJAX 发布后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆