带有Flask的SQLAlchemy不提交

时间:2015-02-22 05:16:04

标签: python flask sqlalchemy flask-sqlalchemy flask-wtforms

当我从网页提交表单时,我无法提交。我需要闪烁的数据,我看到它们在那里并且正确,但在提交时出现了一些问题。我确信我在某个地方犯了一个错误,因为大多数提交工作除了两个。这是两个无效之一。

型号:

class Feedback(db.Model):
    __tablename__ = 'feedback'
    id = db.Column(db.Integer, primary_key = True)
    rate = db.Column(db.Integer)
    comment = db.Column(db.Text())
    sender_id = db.Column(db.Integer)
    receiver_id = db.Column(db.Integer)

形式:

class LeaveFeedbackForm(Form):
    rate =  IntegerField('Rate', validators = [DataRequired(),
                                               NumberRange(min = 1, max = 5, message = 'Rates admitted are only 1,2,3,4,5')])
    comment = TextAreaField('Comment', validators = [DataRequired()])
    submit = SubmitField('Submit')

查看:

@app.route('/leave_feedback/<sender>/<receiver>', methods = ['GET', 'POST'])
def leave_feedback(receiver, sender):
    form = LeaveFeedbackForm()
    rec = int(receiver)
    sen = int(sender)
    if form.validate_on_submit():
        feedback = Feedback( rate = form.rate.data,
                             comment = form.comment.data,
                             receiver_id = rec,
                             sender_id = sen
                            )
        db.session.add(feedback)
        db.session.commit()
        flash('Feedback Left Correctly.')
        return redirect(url_for('index'))
    flash(form.rate.data)
    flash(form.comment.data)
    flash(rec)
    flash(sen)
    return render_template('leave_feedback.html', receiver_id = receiver, sender_id = sender, form = form)

HTML:

{% block content %}
    <div class="row">
        <div class="large-6 columns">
            <h1>Leave Feedback</h1>
        </div>
    </div>

  <form action="" method="post" name="leavefeedback">
      <div class="row">
        <div class="large-6 columns">
            <label>Rate
                {{ form.rate }}
            </label>
        </div>
       </div>

      <div class="row">
        <div class="large-6 columns">
            <label>Comment
                {{ form.comment }}
            </label>
        </div>
       </div>


      <div class="row">
          <div class="large-6 columns">
            <input class="button radius" type="submit" value="Leave Feedback">
         </div>
      </div>

  </form>
{% endblock %}

2 个答案:

答案 0 :(得分:2)

您应该添加else声明:

if form.validate_on_submit():
     ...
else:
    for error in form.errors.itervalues():
        flash(error[0])

然后您将从表单中收到错误消息。

答案 1 :(得分:1)

我弄清楚了我的错误,我只是忘了我的形式:

{{ form.hidden_tag() }}