[英]Submitting for python
So I am trying to make a form that accepts text when submitted and returns submitted text using the /process
function.所以我试图制作一个表单,在提交时接受文本并使用
/process
函数返回提交的文本。
Here is my code for index.html
:这是我的
index.html
代码:
<!DOCTYPE>
<html>
<head>
<title>Whats my name</title>
<h1>What's my name?</h1>
</head>
<body>
<input type="text">
<form action="POST"
>
<p>your name</p><input type="submit">
</body>
</html>
And here is my Python code:这是我的 Python 代码:
from flask import Flask, render_template,redirect # Import Flask to allow us to create our app, and import
# render_template to allow us to render index.html.
app = Flask(__name__) # Global variable __name__ tells Flask whether or not we
# are running the file directly or importing it as a module.
@app.route('/')
def home():
return render_template('index.html')
@app.route('/process',methods=['POST'])
def input():
return redirect ('/')
app.run(debug=True)
To retrieve the name value from your html you'll have to add a tag name
to the input.要从您的 html 中检索名称值,您必须在输入中添加一个标签
name
。
Please see example below, here I named it user_name
:请参见下面的示例,这里我将其命名为
user_name
:
<html>
{...}
<body>
<form action="" method="post">
<input type="text" name="user_name"/>
<p>your name</p>
<input type="submit"/>
</form>
</body>
</html>
Then request the value in your backend Python code然后请求后端 Python 代码中的值
# import the needed request module from Flask
from flask import request
(...)
@app.route('/process', methods=['POST'])
def input():
name = request.form['user_name']
return name
Check this out first: https://www.w3schools.com/tags/att_form_action.asp先看看这个: https : //www.w3schools.com/tags/att_form_action.asp
Action should be "/process" instead of "POST".操作应该是“/process”而不是“POST”。 Method is "POST".
方法是“POST”。 Also you will need input elements in the form to allow user inputs something.
此外,您还需要表单中的输入元素以允许用户输入内容。
The input value can be retrieved on the flask side by request.form['value of name attribute of input']
输入值可以在烧瓶端通过
request.form['value of name attribute of input']
https://www.w3schools.com/tags/tag_input.asp https://www.w3schools.com/tags/tag_input.asp
I would like to recommend you to use https://flask-wtf.readthedocs.io/en/stable/ flask wtf to generate form and retrieve users' input.我想推荐你使用https://flask-wtf.readthedocs.io/en/stable/flask wtf 来生成表单并检索用户的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.