【问题标题】:Trouble Updating Records with WTForms in Flask在 Flask 中使用 WTForms 更新记录时遇到问题
【发布时间】:2015-11-24 18:47:12
【问题描述】:

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

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

观点:

@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 并且被困在一些微不足道的事情上吗?

【问题讨论】:

    标签: python flask wtforms flask-wtforms


    【解决方案1】:

    你很接近,在请求处理程序中,表单的前几行中填充了数据库中Punch 行的上一个版本的数据,试试这样:

    @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 调用中,您可能应该使用烧瓶重定向到在同一网址上呈现模板的不同路由。

    【讨论】:

    • 感谢爬虫!这就像一个魅力,但我仍然担心我不明白表单在什么时候重新填充了 POST 请求中的旧数据。正如您所指出的,我确实希望在 GET 请求中填写旧数据,但我认为 validate_on_submit 调用之后的所有内容都会从表单中获取用户编辑的信息。我的假设是错误的吗?
    • 不客气!在您的请求处理程序中,您实际上是在用模型中的内容覆盖表单中的内容。您加载了模型,然后使用模型中从上次保存时保存的数据重新填充表单。
    猜你喜欢
    • 2018-07-13
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 2022-01-12
    • 2020-07-21
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    相关资源
    最近更新 更多