带有 POST 参数的外部 django 重定向 [英] External django redirect with POST parameters

查看:38
本文介绍了带有 POST 参数的外部 django 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Django 视图中创建一个重定向到外部 url,并在请求中附加一些获取参数.环顾四周,尝试了一番之后,我似乎遇到了障碍.

I'm trying to create a redirect in a Django view to an external url with some get parameters attached to the request. After doing some looking around and some trying around, it seems I have hit a road block.

所以我的观点看起来像这样

So my view looks something like this

def view(request):
    data = get.data(request)
    if something in data:
        return HttpResponseRedirect('example.com')

这是我所能得到的.我知道你在请求 url 中可以指定一些这样的获取参数:

This is as far as I was able to get. I know that you in the request url can specify some get parameters like this:

...
return HttpResponseRedirect('example.com?name=smith&color=brown')

然而,由于一些数据是敏感的,我不希望它最终出现在 url 中.由于它是外部网址,因此我无法使用接受视图参数的 redirect() 快捷方式.那么请教一下,如何完成这样的任务?

However since some of the data is sensitive, I don't want it to end up in the url. Since it's an external url I can't use the redirect() shortcut that accepts views parameters. So pray tell, how does one accomplish such a task?

编辑

在环顾四周并在 IRC 中聊了几句之后,似乎我应该做的是让包含付款信息的 get 参数远离用户,而是将它们作为帖子发送.有人告诉我,您应该也可以使用一些 JS(可能是 jQuery)来做到这一点.这个问题仍然存在,尽管现在有点复杂.如何在 javascript 的帮助下在 django 中创建帖子重定向?

After having done some more looking around, and have chatted a bit in IRC, it seems that what I should do, to keep the get parameter away from users, containing payment info, is to send them as post instead. I was told that you should be able to do it by using some JS as well, possibly jQuery. The question still remains though a bit more complicated now. How would one go about creating post redirect in django with the help of javascript?

第二次编辑

好像我被误导了.感谢您使用重定向协议 DR 解决了这个问题.看起来我在尝试使用重定向来解决这个问题时走错了路.

Seems like I have been misinformed. Thanx for clearing that up with the redirect protocols DR. Looks like I have been going down the wrong path in my attempt of using redirect to solve this problem.

推荐答案

我建议采用以下方法.在您的 Django 视图/模板中,将包含您想要作为隐藏表单元素发布的所有参数的表单返回到浏览器.一旦表单加载,JavaScript 就会将 (POST) 表单提交到您想要的任何位置.

I suggest the following approach. In your Django view/template return form to the browser with all the parameters that you want to post as hidden form elements. As soon as the form loads the JavaScript will submit (POST) form to where ever you want.

查看:

from django.shortcuts import render_to_response

def view(request):
    return render_to_response('test.html', { 'foo': 123, 'bar': 456 })

模板:

<html>
<head>
    <title>test</title>
     <script type="text/javascript">
     function load()
     {
          window.document.test.submit();
          return;
     }
     </script>
</head>
<body onload="load()">
<form name="test" method="post" action="http://www.example.com">
    <input type="hidden" name="foo" value={{ foo }} />
    <input type="hidden" name="bar" value={{ bar }} />
</form>
</body>
</html>

这篇关于带有 POST 参数的外部 django 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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