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

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

问题描述

我试图在Django视图中创建一个重定向到一个外部url,其中一些get参数附加到该请求。在做一些环顾四周的尝试后,似乎已经打了一个路障。

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可以指定一些get参数,如下所示:

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。因为它是一个外部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参数远离用户,包含支付信息,就是将它们作为post发送。我被告知你应该能够使用一些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?

第二个修改

似乎已经被误导了。 Thanx用于清除重定向协议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>

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

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