[英]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.