[英]Submitting for python
所以我试图制作一个表单,在提交时接受文本并使用/process
函数返回提交的文本。
这是我的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>
这是我的 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)
要从您的 html 中检索名称值,您必须在输入中添加一个标签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>
然后请求后端 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
先看看这个: https : //www.w3schools.com/tags/att_form_action.asp
操作应该是“/process”而不是“POST”。 方法是“POST”。 此外,您还需要表单中的输入元素以允许用户输入内容。
输入值可以在烧瓶端通过request.form['value of name attribute of input']
https://www.w3schools.com/tags/tag_input.asp
我想推荐你使用https://flask-wtf.readthedocs.io/en/stable/flask wtf 来生成表单并检索用户的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.