简体   繁体   English

POST请求中的Django GET请求参数

[英]Django GET request params from POST request

I have a form that you need to fill out using a POST request, but I want to the result to be a redirection to various pages on my site. 我有一个表单,您需要使用POST请求填写,但我希望结果是重定向到我网站上的各个页面。 Sometimes I want the user to be redirected to a profile page, sometimes to a purchase page. 有时我希望将用户重定向到个人资料页面,有时又重定向到购买页面。

So I put the redirect URI in the GET params of the URL of the form page that POSTs: /form/?redirect_uri=/payments/ 因此,我将重定向URI放在POST表单页的URL的GET参数中:/ form /?redirect_uri = / payments /

The url is correct upon landing on the forms page, with the appropriate redirect_uri. 使用适当的redirect_uri,网址在登陆表单页面时是正确的。 However, when I fill out the form and POST, Django doesn't seem to think there are any params in the GET request. 但是,当我填写表单和POST时,Django似乎并不认为GET请求中有任何参数。

How can I get those params from the URL out on a POST request? 如何在POST请求中从URL获取这些参数?

EDIT: 编辑:

def form_view(request):
    if request.method == "POST":
        # Do stuff with form validation here
        redirect_uri = "/home/"
        if "redirect_uri" in request.GET:
            redirect_uri = request.GET['redirect_uri']
            if redirect_uri == request.path:
                # avoid loops
                redirect_uri = "/home/"
        return redirect(redirect_uri)

This form page is loaded by accessing this url: 通过访问以下网址来加载此表单页面:

/form/?redirect_uri=/payments/

Form: 形成:

<form action="/form/" method="POST" class="wizard-form" enctype="application/x-www-form-urlencoded" autocomplete="off">
            {% csrf_token %}
            <fieldset class="form-group">
                <label for="email">Email address</label>
                <input name="email" type="text" class="form-control" id="email" placeholder="Enter email or username" required>
            </fieldset>
            <button type="submit" class="btn btn-primary">Go</button>
        </form>

In the POST request - ie when you submit the form - the url will be what you specified in the 'action' attribute in the form. 在POST请求中(即,当您提交表单时),URL将是您在表单的'action'属性中指定的网址。 If the parameters you require are not there, your view will not get them. 如果所需的参数不存在,则视图将无法获取它们。

So either update your forms's action attribute to have these parameters or alternatively you can add thin in the form itself (as hidden inputs). 因此,要么更新表单的action属性以具有这些参数,要么可以在表单本身中添加thin(作为隐藏的输入)。

In GET request you will get attributes from url that you see in the browser. 在GET请求中,您将从浏览器中看到的URL中获取属性。

Your form has to be modified as follows: 您的表格必须进行如下修改:

<form action="/form/?redirect_uri={{ request.GET.redirect_url}}"
    method="POST" class="wizard-form"
    enctype="application/x-www-form-urlencoded" autocomplete="off">

Permit me to also suggest a slight optimization to your view 请允许我也建议您对视图进行一点优化

def form_view(request):
    if request.method == "POST":
        # Do stuff with form validation here
        redirect_uri = request.GET.get('redirect_uri',"/home/")
        if "redirect_uri"  == request.path:
                # avoid loops
                redirect_uri = "/home/"
        return redirect(redirect_uri)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM