从具有 SQLAlchemy 名称的复选框中获取表单

Get form from a checkbox that has a name from a SQLAlchemy

我正在为一个项目使用 flask 和 sqlalchemy。 如果选中该复选框,则尝试更新用户的列(整数)'prezenta'。 我如何获得这些复选框的形式?

class Users(UserMixin, db.Model):
   id = db.Column(db.Integer, primary_key = True)
   Nume = db.Column(db.String(100))
   Prenume = db.Column(db.String(100))
   prezenta = db.Column(db.Integer)

   def __init__(self, Nume, Prenume, prezenta):
      self.Nume = Nume
      self.Prenume = Prenume 
      self.prezenta = prezenta

我在 html 中有一个 table,里面有一个人的列表,我用 jinja 制作的,每一行还有一个复选框输入,带有人名 ID。 如果我想更新 'prezenta' 列,我的 python 代码应该如何显示,如果选中人员 ID 复选框,将其增加 1?

<form class="form" action="" method="post">
    <div>
        <table class="table">
            <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Nume</th>
                    <th scope="col">Prenume</th>
                    <th scope="col">Prezenta</th>
                </tr>
            </thead>
            <tbody>
                {% for user in Users %}
                <tr>
                    <th scope="row">{{ user.id }}</th>
                    <td>{{ user.Nume }}</td>
                    <td>{{ user.Prenume }}</td>
                    <td>
                        <input type="checkbox" name="{{ user.id }}" />
                        {{ user.prezenta }}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        <input
            type="submit"
            name="submit"
            class="btn btn-info btn-md"
            value="Submit"
        />
    </div>
</form>

从复选框中给出答案 0 或您指定的值

也许您想使用单选按钮?

也将其传递给烧瓶使用:

chqbox = request.args.get('user.id')

和一个“if”来检查复选框内的值