浏览器是否应该重定向到Flask POST请求的URL?

huangapple go评论76阅读模式
英文:

Are browsers supposed to redirect to the URL of a Flask POST request?

问题

以下是您要的代码部分的中文翻译:

# app.py
从flask导入Flask请求渲染模板

app = Flask(__name__)

@app.route('/'methods=['GET'])
def index():
    返回渲染模板('form.html')

@app.route('/output'methods=['POST'])  # 仅接受POST请求
def output():
    返回'A POST request was made; input1 = ' + request.form.get('input1')
    # 当上述内容返回时,浏览器导航到/output……这是怎么回事?

if __name__ == '__main__':
    app.run()
<!-- form.html -->
<form action="/output" method="post">
输入1:<input type="string" name="input1">
<input type="submit">
</form>

请注意,我已经移除了您提供的非代码部分的内容,只保留了代码本身的翻译。

英文:

The index page of my Flask app contains an HTML form (to receive user input), and this HTML form sends the user input to another endpoint (/output) via a POST request, where it's then processed and a simple output is returned. But curiously, when the output is returned, it's returned at the /output endpoint (url). But how do I make sense of this, given that navigating to a new URL represents a GET request? Shouldn't a POST request not change the URL in your browser at all? I also defined by /output endpoint to only accept POST requests, so I know there's no GET requests going on behind the scenes here. (For context, I'm running Google Chrome on Windows.)

Does Flask perhaps automatically return output at the endpoint specified, regardless of whether you made a POST or a GET request to that endpoint(url)? If that's the case, then not all URL changes in your browser's search bar represent GET requests, apparently. I'm just stumped on what to make of this since I never redirected or made a GET request to the /output url, and I'm brand new to webdev so would greatly appreciate any insight. Thanks so much!!

Here's an MRE:

app.py

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route(&#39;/&#39;, methods=[&#39;GET&#39;])
def index():
    return render_template(&#39;form.html&#39;)

@app.route(&#39;/output&#39;, methods=[&#39;POST&#39;])  # Only accepts POST requests
def output():
    return &#39;A POST request was made; input1 = &#39; + request.form.get(&#39;input1&#39;)
    # The browser navigates to /output when the above is returned...how?

if __name__ == &#39;__main__&#39;:
    app.run()

form.html:

&lt;form action=&quot;/output&quot; method=&quot;post&quot;&gt;
Input 1: &lt;input type=&quot;string&quot; name=&quot;input1&quot;&gt;
&lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;

答案1

得分: 1

POST请求应该不会在浏览器中完全更改URL,而是意味着数据不会显示在地址栏中(作为URL的一部分),但并不表示URL将完全不会更改。

英文:

> Shouldn't a POST request not change the URL in your browser at all?

POST means data will not appear in bar (as part of URL), not that URL will not change at all.

huangapple
  • 本文由 发表于 2023年6月30日 02:32:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583772.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定