External django redirect with POST parameters

混江龙づ霸主 提交于 2019-11-28 06:55:34

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.

View:

from django.shortcuts import render_to_response

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

Template:

<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>

GET parameters always go in the URL, that's what makes them GET parameters.

It is not possible to redirect using POST parameters (which don't go in the URL) - this is a restriction of HTTP, not Django.

I don't agree with Daniel, there's a way around the HTTP limitation of redirecting only with get params. what i am thinking of is:

  1. Redirect to a GET destination with the params you want.
  2. On the GET page, setup a form with the params you forwarded
  3. Auto submit the form.

That's what most file download services do and sometimes even payment pages (Paypal and such). it is indeed a bit ugly (1 more hop) but has it's own benefits.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!