12

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?

2
  • Most likely want WTForms - Flask-wtf docs at flask-wtf.readthedocs.io/en/latest Commented May 20, 2016 at 12:40
  • Classic incorrect closing. The "already answered" question doesn't address textarea input AT ALL. :( Commented Apr 17, 2024 at 17:14

1 Answer 1

26

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'])
Sign up to request clarification or add additional context in comments.

2 Comments

This solution does not work. Is there perhaps another dependency within the HTML that is missing? `url_for('submit')
Make sure that you place the form.html file into a directory called templates. "render_template" is looking for that directory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.