首页 > 解决方案 > 从带有烧瓶的for循环中显示的列表中将一些选定的项目保存到mysql

问题描述

我在一个使用flask的应用程序上,我检索了一个实体列表,因为没有分析我在表格中显示给用户的文本,这要归功于通过html文件的for循环,用户在验证返回的信息后,它必须验证并在数据库中保存一些数据并留下其他数据。我的问题是我无法管理批量验证,同一页面上的多个验证,因为它是在循环级别检索的单个变量。任何帮助将不胜感激。

代码烧瓶:

  personEntities = (set([e.text for e in doc.ents if e.label_ == "PER"]))
    locationEntities = list(set([e.text for e in doc.ents if e.label_ == "LOC"]))
    organizationEntities = list(set([e.text for e in doc.ents if e.label_ == "ORG"]))
    return render_template("extract_full.html", perso =personEntities, loca = locationEntities, orga = organizationEntities)

对于代码 HTML :

<table>
  <tr>
    <th>Entités</th>
    <th>Categorie</th>
    <th>Action</th>
  </tr>
      {% for personne in perso %}
          <tr>
            <td>
                    {{ personne }}
            </td>
            <td>Personne</td>

            <td>
                <form method="post">
                    <button type="submit" name = "person" class="btn waves-effect waves-light">Valider</button>
                </form>
            </td>

          </tr>
      {% endfor %}
</table>

为了保存在数据库中:

if request.method == "POST":
                    entite = ''
                    if (personEntities):
                        newPerson = Personne(nomFamile=personEntities)
                        db.session.add(newPerson)
                        db.session.commit()

标签: htmlflasksqlalchemyentities

解决方案


推荐阅读