在 Flask 中使用 WTForms 更新记录时遇到问题

Trouble Updating Records with WTForms in Flask

我正在用 Flask 编写一个应用程序,它应该用作计时的虚拟打卡时钟,但我在更新记录时遇到了问题。

对于现有的签到或签退记录(称为打卡),我希望能够允许具有 'manager' 权限的用户编辑某些属性。这是我目前所拥有的:

观点:

@app.route('/punch/<punchid>', methods=['GET', 'POST'])
@login_required
def punch(punchid):
    if g.user.is_manager:
        punch = Punch.query.get(punchid)
        form = PunchForm()
        form.notes.data = punch.notes
        form.timestamp.data = punch.timestamp
    else:
        flash('You are not authorized to access this function.')
        return redirect(url_for('user', nickname=g.user.nickname))
    if form.validate_on_submit():
        punch.notes = form.notes.data
        punch.timestamp = form.timestamp.data
        print(form.errors)
        db.session.commit()
        flash('Your changes have been saved.')
        return render_template('report.html', punches=Punch.query.order_by(desc(Punch.timestamp)).all())
    else:
        flash('Please check to see that you have entered information correctly.')
    return render_template('punch.html', form=form, punch=punch)

形式:

class PunchForm(Form):
    notes = StringField('notes', validators=[DataRequired()])
    timestamp = DateTimeField('timestamp', format="%Y-%m-%d %H:%M")

模板:

<h1>Edit this Clock-Punch:</h1>
  <form action="" method="post" name="punch">
      {{form.hidden_tag()}}
      <table>
          <tr>
              <td>Employee:</td>
              <td>{{punch.author.nickname}}</td>
          </tr>
          <tr>
              <td>Employee Number:</td>
              <td>{{punch.author.employee_number}}</td>
          </tr>
          <tr>
              <td>In or Out:</td>
              <td>{{punch.in_or_out}}</td>
          </tr>
          <tr>
              <td>Timestamp:</td>
              <td>{{ form.timestamp(size=48) }}</td>
          </tr>
          <tr>
              <td>Notes:</td>
              <td>{{ form.notes(size=128) }}</td>
          </tr>
          <tr>
              <td></td>
              <td><input type="submit" value="Save Changes"></td>
          </tr>
      </table>
  </form>

问题: 当我点击提交按钮时,一切似乎都工作正常。我的服务器甚至在 POST 上给我一个 HTTP 200 响应。然而,当我再次查看冲头时,数据仍然没有改变。我是不是在这里完全是 n00b 并且陷入了一些琐碎的事情?

你很接近,在请求处理程序中,表单的前几行中填充了来自 Punch 行的 previous 版本的数据数据库,尝试这样的事情:

@app.route('/punch/<punchid>', methods=['GET', 'POST'])
@login_required
def punch(punchid):
    if not g.user.is_manager:
        flash('You are not authorized to access this function.')
        return redirect(url_for('user', nickname=g.user.nickname))

    # only for the initial GET request do you want to load
    # the original data from the model into the form
    punch = Punch.query.get(punchid)
    # should throw in a check to make sure this exists, and abort(404) if not...
    if request.method == 'GET':
        form = PunchForm()
        form.notes.data = punch.notes
        form.timestamp.data = punch.timestamp
    if form.validate_on_submit():
        punch.notes = form.notes.data
        punch.timestamp = form.timestamp.data
        print(form.errors)            
        db.session.commit()
        flash('Your changes have been saved.')
        return render_template('report.html', punches=Punch.query.order_by(desc(Punch.timestamp)).all())
    else:
        flash('Please check to see that you have entered information correctly.')
    return render_template('punch.html', form=form, punch=punch)

此外,在 validate_on_submit 调用中,您可能应该使用 flask 重定向到不同的路由,在相同的 url 上呈现模板。