【发布时间】: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