Flash 仅从 html 表单获取一个值出售武器

问题描述 投票:0回答:1

我有以下

html
代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sell Weapons</title>
</head>
<body>
<form action="{{ url_for('predict')}}"method="post">
    <label for="comment1">evaluate first product</label><br>
    <input type="text" id="comment1" name="comment" required="required"><br>
      <label for="comment2">evaluate second product</label><br>
    <input type="text" id="comment2" name="comment" required="required"><br>
      <label for="comment">evaluate third product</label><br>
    <input type="text" id="comment" name="comment" required="required"><br>
     <input type="submit" value="Submit">
</form>

 <h4 style="color:Violet;">
   {{ prediction_text }} </h4>

</body>
</html>

当我们运行它时,我得到以下输出:

在这种形式中我写了三个文本,但它只向我显示了一个,这里是相应的Python代码与

Flask

import numpy as np
from flask import Flask,request,jsonify,render_template
import pickle
from transformers import pipeline

app = Flask(__name__)
@app.route('/')
def home():
    return render_template("Commenting.html")

@app.route('/predict',methods=['POST'])
def predict():
    text =  [x for x in request.form.values()]
    # text=list(text)
    return render_template('Commenting.html', prediction_text=text)
    # sentiment_pipeline = pipeline("sentiment-analysis")
    # result =sentiment_pipeline(text)[0]
    # if result['label']=='POSITIVE':
    #     return render_template('Commenting.html',prediction_text=f'emotion of comment is positive')
    # else:
    #     return render_template('Commenting.html', prediction_text=f'emotion of comment is not positive')

if __name__ =="__main__":
    app.run()

您能告诉我为什么我只能访问第一个文本吗?剩下的人在哪里?

python html flask
1个回答
1
投票

通常,当您提交表单时,浏览器会收集数据并将其发送到服务器。但是,由于所有三个输入都具有相同的名称属性 (

name="comment"
),因此这些输入中的数据会相互覆盖,并且仅保留最后一个。

您需要为每个输入字段指定一个唯一的名称。

类似这样的:

<input type="text" id="comment1" name="comment1" required="required"><br>
<input type="text" id="comment2" name="comment2" required="required"><br>
<input type="text" id="comment3" name="comment3" required="required"><br>
© www.soinside.com 2019 - 2024. All rights reserved.