我有以下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>

when we run it, I get following output : enter image description here

在这个表单中,我写了三个文本,但它只显示了一个,这里是对应的Python代码,

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()

你能告诉我为什么我只能访问第一个文本吗?其他人呢?

推荐答案

通常,当您提交表单时,浏览器会收集数据并将其发送到服务器.然而,由于所有三个输入具有相同的名称属性(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>

Python相关问答推荐

GL pygame无法让缓冲区与vertextPointer和colorPointer一起可靠地工作

Pandas 填充条件是另一列

Python中的嵌套Ruby哈希

将两只Pandas rame乘以指数

如何让Flask 中的请求标签发挥作用

从dict的列中分钟

为什么默认情况下所有Python类都是可调用的?

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

删除字符串中第一次出现单词后的所有内容

运输问题分支定界法&

Stacked bar chart from billrame

从嵌套的yaml创建一个嵌套字符串,后面跟着点

matplotlib图中的复杂箭头形状

在二维NumPy数组中,如何 Select 内部数组的第一个和第二个元素?这可以通过索引来实现吗?

如何在Python 3.9.6和MacOS Sonoma 14.3.1下安装Pyregion

判断Python操作:如何从字面上得到所有decorator ?

GPT python SDK引入了大量开销/错误超时

Python日志(log)库如何有效地获取lineno和funcName?

递归链表反转与打印语句挂起

为什么在不先将包作为模块导入的情况下相对导入不起作用