[英]How to append user input from front end (i.e. HTML form) to the url
I have a basic HTML form which includes a text box and a submit button.我有一个基本的 HTML 表单,其中包括一个文本框和一个提交按钮。 Let us say, the user types an input to the input box.
假设用户在输入框中输入了一个输入。 I want to append that input to the existing URL of the home page.
我想将该输入附加到主页的现有 URL。
Example: The URL of the home page be "localhost/home" and the user inputs "data" into the text box.示例:首页的 URL 为“localhost/home”,用户在文本框中输入“data”。
On the click of submit button, the URL should append the input and look something like: "localhost/home/data" (upon displaying the result fetched from backend using flask).单击提交按钮时,URL 应附加输入并类似于:“localhost/home/data”(显示使用 Flask 从后端获取的结果时)。
So, technically when I click submit, I want to be redirected to "localhost/home/data".因此,从技术上讲,当我单击提交时,我希望被重定向到“localhost/home/data”。
I'm using the below HTML form.我正在使用下面的 HTML 表单。
<form action="/home" method="POST">
<input type=text name="name">
<input type=submit name="submit">
</form>
and using this as the flask code snippet:并将其用作烧瓶代码片段:
@app.route('/home', methods=['POST', 'GET'])
def basic():
if request.method == 'POST':
path = (my path to json file)
with open(path) as f:
data = json.load(f)
format_list=data[name]
return format_list
return render_template('temp.html')
How can I get the inputted parameters from the form appended to the URL?如何从附加到 URL 的表单中获取输入的参数? Thanks!
谢谢!
When you use form submission with flask, the form is exposed on the flask object as a flask.request.form
python dictionary.当您将表单提交与flask 一起使用时,表单在flask 对象上公开为
flask.request.form
python 字典。 From here, you can access what was POSTed to your endpoint:从这里,您可以访问发布到您的端点的内容:
@app.route('/home', methods=['POST', 'GET'])
def basic():
if request.method == 'POST':
path = request.form['name']
Do note you should be giving your input fields id
attributes:请注意,您应该提供输入字段
id
属性:
<input id="name" name="name">
If you want to append this to a path or something of that nature, then you can further look into flask.redirect
.如果您想将此附加到路径或类似性质的内容,那么您可以进一步查看
flask.redirect
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.