首页 > 解决方案 > Flask 从带有文本区域的表单输入数据

问题描述

我想从 html 文件中输入数据(文本区域)并在 Python 中处理它,我制作了这样的表格:

<form action="/" method="post">
    <div class="postText">
    <textarea name="" id="text" cols="30" rows="15" placeholder="Insert the post here">

    </textarea>
    </div>

    <button type="submit">Go!</button>
    </form>

在烧瓶中,我有以下路线:

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        sequences = ['This is a gaming sentence']
        prediction = clf.predict(sequences)[0].title()
        return render_template('index.html', prediction=prediction)
    else:
        return render_template('index.html',
                               prediction='Predictions will appear here!')

我想用从HTML 中sequences的标记输入的数据替换 POST 方法上的硬编码变量 。textarea

谢谢你的帮助!

标签: pythonhtmlformsflasktextarea

解决方案


你可以添加这个。
索引.html

<textarea name="form-text" id="text" cols="30" rows="15" placeholder="Insert the post here">

应用程序.py

@app.route('/', methods=['GET', 'POST'])
def index():
    sequences = ['This is a gaming sentence']
    if request.method == 'POST':
        sequences = request.form.get("form-text")
        prediction = clf.predict(sequences)[0].title()
        return render_template('index.html', prediction=prediction)
    else:
        return render_template('index.html',
                               prediction='Predictions will appear here!')

推荐阅读