将数据从文本框发送到Flask? [英] Send Data from a textbox into Flask?
问题描述
除非您想做更复杂的事情,否则将HTML表单提供给Flask的数据非常容易。 / p>
- 创建一个接受POST请求的视图(
my_form_post
)。
- 访问词典中的表单元素
request.form
。
$ b $ $ b
< form method =POST>
< input name =text>
< input type =submit>
< / form>
from flask import Flask ,request,render_template
app = Flask(__ name__)
$ b $ app_route('/')
def my_form():
return render_template ('my-form.html')
@ app.route('/',methods = ['POST'])
def my_form_post():
text = request .form ['text']
processed_text = text.upper()
return processed_text
这是关于访问请求数据的Flask文档。
如果您需要更复杂的表单需要验证,那么您可以查看 WTForms 和如何把它们和Flask整合起来。
注意:除非你有ha有任何其他的限制,你根本不需要JavaScript 发送你的数据(尽管你可以使用它)。
I was wondering if there was a way to take something from a text box in the HTML, feed it into flask, then parse that data with Python. I was thinking this might involve some JS but I could be wrong. Any ideas?
Unless you want to do something more complicated, feeding data from a HTML form into Flask is pretty easy.
- Create a view that accepts a POST request (
my_form_post
). - Access the form elements in the dictionary
request.form
.
templates/my-form.html
:
<form method="POST">
<input name="text">
<input type="submit">
</form>
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('my-form.html')
@app.route('/', methods=['POST'])
def my_form_post():
text = request.form['text']
processed_text = text.upper()
return processed_text
This is the Flask documentation about accessing request data.
If you need more complicated forms that need validation then you can take a look at WTForms and how to integrate them with Flask.
Note: unless you have any other restrictions, you don't really need JavaScript at all to send your data (although you can use it).
这篇关于将数据从文本框发送到Flask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!