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

时间:2015-11-24 18:47:13

标签: python flask wtforms flask-wtforms

我正在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并且陷入了一些微不足道的事情吗?

1 个答案:

答案 0 :(得分:0)

您很接近,在请求处理程序中,表单将在前几行填充来自数据库中@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

此外,在{{1}}调用中,您应该使用烧瓶重定向到在同一网址上呈现模板的其他路径。