英文:
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('/', methods=['GET'])
def index():
return render_template('form.html')
@app.route('/output', methods=['POST']) # Only accepts POST requests
def output():
return 'A POST request was made; input1 = ' + request.form.get('input1')
# The browser navigates to /output when the above is returned...how?
if __name__ == '__main__':
app.run()
form.html:
<form action="/output" method="post">
Input 1: <input type="string" name="input1">
<input type="submit">
</form>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论