gpt4 book ai didi

jquery - 带 POST 参数的外部 django 重定向

转载 作者:行者123 更新时间:2023-12-03 21:54:32 25 4
gpt4 key购买 nike

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

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

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

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

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

但是,由于某些数据是敏感的,我不希望它最终出现在网址中。由于它是外部 url,我无法使用接受 View 参数的 redirect() 快捷方式。那么请问,如何完成这样的任务呢?

编辑

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

第二次编辑

看来我被误导了。感谢您使用重定向协议(protocol) DR 解决了这个问题。看来我在尝试使用重定向来解决这个问题时走错了路。

最佳答案

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

查看:

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>

关于jquery - 带 POST 参数的外部 django 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1345892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com