将数据从文本框发送到Flask? [英] Send Data from a textbox into Flask?

查看:1561
本文介绍了将数据从文本框发送到Flask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以从HTML中的文本框中提取某些内容,然后将其填充到瓶子中,然后使用Python解析这些数据。我想这可能涉及一些JS,但我可能是错的。任何想法?

解决方案

除非您想做更复杂的事情,否则将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆