Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve text from textarea in Flask [duplicate]

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?

like image 236
filiphl Avatar asked May 20 '16 11:05

filiphl


1 Answers

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'])
like image 117
MilkeyMouse Avatar answered Oct 22 '22 23:10

MilkeyMouse