简体   繁体   中英

Trouble Updating Records with WTForms in Flask

I'm writing an app in Flask that's supposed to serve as a virtual punch-clock for timekeeping, but I'm having trouble updating records.

For existing clock-in or clock-out records (called punches), I want to be able to allow a user with 'manager' permissions to edit certain attributes. Here's what I have so far:

the view:

@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)

the form:

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

the template:

<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>

the problem: Everything appears to work fine when I hit the submit button. My server even gives me an HTTP 200 response on the POST. Yet, when I view the punch again, the data remains unchanged. Am I being a total n00b here and getting stuck on something trivial?

You are close, in the request handler, the form is getting populated in the first few lines with the data from the previous version of the Punch row from the database, try something like this:

@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)

Also, in the validate_on_submit call, you should probably use a flask redirect to a different route that render a template on the same url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM