从Flask的textarea中检索文本 [英] Retrieve text from textarea in Flask

查看:809
本文介绍了从Flask的textarea中检索文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在textarea(HTML)中编写多行文本,并在python中检索这个文本以使用Flask进行处理。或者,我希望能够在表单中编写多行文本。我对使用JS没有任何线索,所以不会帮助我。
我该怎么做呢?

解决方案

使用form和textarea渲染模板。使用 url_for 将窗体指向将处理数据的视图。访问 request.form 中的数据。
$ b templates / form.html


pre $ < form action ={{url_for('submit')}}方法= POST >
< textarea name =text>< / textarea>
< input type =submit>
< / form>

app.py

  from flask import Flask,request,render_template 
$ b $ app = Flask(__ name__)

@ app.route('/')
def index():
return render_template('form.html')

@ app.route('/ submit',methods格式(request.form ['text'])
def submit():
return' >


I would like to be able to write a multi-line text in a textarea (HTML), and retrieve this text in python for processing using Flask. Alternatively, I would like to be able to write a multi-line text in a form. I have no clue on using JS, so that won't help me. How am I to go about doing that?

解决方案

Render a template with the form and textarea. Use url_for to point the form at the view that will handle the data. Access the data from request.form.

templates/form.html:

<form action="{{ url_for('submit') }}" method="post">
    <textarea name="text"></textarea>
    <input type="submit">
</form>

app.py:

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('form.html')

@app.route('/submit', methods=['POST'])
def submit():
    return 'You entered: {}'.format(request.form['text'])

这篇关于从Flask的textarea中检索文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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