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?
-
Most likely want WTForms - Flask-wtf docs at flask-wtf.readthedocs.io/en/latestBusturdust– Busturdust2016-05-20 12:40:50 +00:00Commented May 20, 2016 at 12:40
-
Classic incorrect closing. The "already answered" question doesn't address textarea input AT ALL. :(Mark Allen– Mark Allen2024-04-17 17:14:49 +00:00Commented Apr 17, 2024 at 17:14
Add a comment
|
1 Answer
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'])
2 Comments
bmc
This solution does not work. Is there perhaps another dependency within the HTML that is missing? `url_for('submit')
Malachi Bazar
Make sure that you place the form.html file into a directory called templates. "render_template" is looking for that directory.