【问题标题】:Flask Inputting Data From Form with Text Area [duplicate]Flask从带有文本区域的表单输入数据[重复]
【发布时间】:2021-12-04 19:59:04
【问题描述】:

我想从 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>

在 Flask 中,我有以下路线:

@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!')

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

感谢您的帮助!

【问题讨论】:

    标签: python html forms flask textarea


    【解决方案1】:

    你可以添加这个。
    index.html

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

    app.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!')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-05
      • 2020-11-25
      • 1970-01-01
      • 2021-08-05
      • 2012-03-16
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多