简体   繁体   English

Django - 从 javascript 传递数据以查看并重定向到新的 url

[英]Django - Pass data from javascript to view & redirect to a new url

On my webpage there are 2 buttons - A & B. I want to capture the button clicked by the user in front end & send it back to the server so that I can generate the appropriate token for the user.在我的网页上有 2 个按钮 - A 和 B。我想捕获用户在前端单击的按钮并将其发送回服务器,以便我可以为用户生成适当的令牌。 Then redirect the user to the right url - "/A" or "/B" depending on what they clicked.然后将用户重定向到右侧 url - “/A”或“/B”,具体取决于他们单击的内容。

urls.py网址.py

    path('buttonTest/', buttonView), # Initially testing with one url

Views.py视图.py

def buttonView(requests):
    if requests.method == 'POST':
        clicked_button_val = requests.POST.get('clicked_button')
        # generate auth token
        render(requests, 'test.html')

test.html测试.html

<form id="testform">
    <button name="subject" id="A" type="submit" value="A">A</button>
    <button name="subject" id="B" type="submit" value="B">B</button>
</form>
<script>
$("button").click(function() {
    $.ajax({
        type: "POST",
        data: { 
            "clicked_button": $(this).val(),
        },
        success: function(result) {
            alert('ok');
        },
        error: function(result) {
            alert('error');
        }
    });
});
</script>

I am able to successfully pass the data from front-end to views.我能够成功地将数据从前端传递到视图。 But the url doesn't redirect to /buttonTest.但是 url 不会重定向到 /buttonTest。 If I add redirect in html like this,如果我像这样在 html 中添加重定向,

<button name="subject" id="A" type="submit" value="A" onclick="location.href='/buttonTest'">A</button>

then the request in views.py becomes a GET request & is not able to access what the user clicked.那么views.py中的请求变成了一个GET请求并且不能访问用户点击的内容。

What I want to do is capture the button clicked by user via a POST request & then redirect the user to a new url & pass the data captured in the POST request to the new url & view.我想要做的是捕获用户通过 POST 请求单击的按钮,然后将用户重定向到新的 url 并将 POST 请求中捕获的数据传递给新的 url 并查看。

Most likely your ajax request is not even used, because your buttons have type="submit" and you never prevent the form submission.很可能甚至没有使用您的 ajax 请求,因为您的按钮具有type="submit"并且您永远不会阻止表单提交。 The simplest method would be to set a different name attribute on both the buttons and forego using JavaScript:最简单的方法是使用 JavaScript 在按钮和放弃上设置不同的name属性:

<form id="testform">
    <button name="submit_A" id="A" type="submit" value="A">A</button>
    <button name="submit_B" id="B" type="submit" value="B">B</button>
</form>

Now in the view:现在在视图中:

def buttonView(request): # request should be singular(naming convention)!!
    if request.method == 'POST':
        if 'submit_A' in requests.POST:
            # generate auth token for button A and redirect
        elif 'submit_B' in requests.POST:
            # generate auth token for button B and redirect
        render(requests, 'test.html')

You cannot include data in a redirect since a redirect triggers a GET request.您不能在重定向中包含数据,因为重定向会触发 GET 请求。

You can however add a formaction attribute to submit inputs to submit the same form to different URLs但是,您可以添加一个formaction属性来提交输入以将相同的表单提交到不同的 URL

<input type="submit" formaction="/handle-a/" value="A">
<input type="submit" formaction="/handle-b/" value="B">

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

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