从Flask的textarea中检索文本 [英] Retrieve text from textarea in Flask
问题描述
我该怎么做呢?
使用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
def submit():
$ b $ app = Flask(__ name__)
@ app.route('/')
def index():
return render_template('form.html')
@ app.route('/ submit',methods格式(request.form ['text'])
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屋!