Flask WTForms validate_on_submit不会执行

时间:2018-02-16 01:21:19

标签: python validation flask wtforms flask-wtforms

下面的烧瓶形式渲染得很好,但它接受任何(或没有)输入并始终在提交时重定向到目标URL。本周寻找解决方案出现了类似案例,但其中大部分都与缺少CSRF令牌有关(https://github.com/billziss-gh/oauth2-helper)。通过form.hidden_​​tag()肯定有一个CSRF令牌,当Werkzeug调试器打开时,form.validate()和form.validate_on_submit()都返回输出。但是,如果源代码中包含print语句,则两个语句都不会生成输出。

以下是模型中的表格:

from wtforms.validators import InputRequired, NumberRange
from flask_wtf import FlaskForm

class revise_task(task_abt):
ID = IntegerField("Record ID", validators=[InputRequired()])
date = DateField('Date', validators=[InputRequired()], format="%Y-%m-%d")

class revise_punch(FlaskForm):
punchtype = SelectField('Punch Type', validators=[InputRequired()], choices 
= [('CLOCK IN AM', 'CLOCK IN AM'), ('CLOCK OUT LUNCH','CLOCK OUT LUNCH'), 
('CLOCK IN LUNCH', 'CLOCK IN LUNCH'), ('CLOCK OUT PM', 'CLOCK OUT PM'), 
('SICK TIME', 'SICK TIME')])
date = DateTimeField("Timestamp", format='%Y-%m-%d %H:%M:%S')
ID = IntegerField("Record ID", validators=[InputRequired()])

以下是视图中的表单:

@puncher.route('/review', methods=['POST','GET'])
@oauth2.required
def request_changes():
    task_form = revise_task(request.form)
    punch_form = revise_punch(request.form)

    task_form.project.choices = [(i[0],str(i[0])) for i in 
    db.session.query(distinct(Task.Project))]
    task_form.task.choices=[(i[0],str(i[0])) for i in 
    db.session.query(distinct(Task.Task))]

    name = session['profile']['displayName'].upper()

    ps = pd.read_sql_query(Punch.query.filter_by(Name=name).order_by(Punch.Timestamp.desc()).limit(32).statement, db.session.bind)
    ts = pd.read_sql_query(Task.query.filter_by(Name=name).order_by(Task.Date.desc()).limit(32).statement, db.session.bind)
    sick = pd.read_sql_query(Sick.query.filter_by(Name=name).order_by(Sick.Date.desc()).limit(5).statement, db.session.bind)

    tables = {'Tasks':ts.to_html(classes="table table-striped table-condensed", index=False).replace("dataframe ",""), 'Timecard':ps.to_html(classes="table table-striped table-condensed",index=False).replace("dataframe ",""), 'Sick time':sick.to_html(classes="table table-striped table-condensed", index=False).replace("dataframe ","")}

    if task_form.validate_on_submit():
        record = Rev_Task(Date=request.task_form['date'], Name=name,Project=request.task_form['project'], Task=request.task_form['task'], Num_hours=request.task_form['hours'], ID_target=request.task_form['ID'])
        db.session.add(record)
        db.session.commit()

        return redirect(url_for('puncher.review_confirm'))

    elif punch_form.validate_on_submit():
        record = Rev_Punch(Timestamp=request.punch_form['date'], Name=name, 
        Punch_Type=request.punch_form['punchtype'], 
        ID_target=request.punch_form['ID'])
        db.session.add(record)
        db.session.commit()

        return redirect(url_for('puncher.review_confirm'))

return render_template("review.html", tables=tables, task_form=task_form, punch_form=punch_form)

这是模板:

<div class="form-group" align="center">
<form action="{{ url_for('puncher.review_confirm') }}" method="POST">
    {{ task_form.hidden_tag() }}
    <p>
        {{ task_form.date.label }}<br>
        {{ task_form.date(class_="form-control", type_="date") }}
    </p>
    <p>
        {{ task_form.project.label }}<br>
        {{ task_form.project(class_="form-control") }}
    </p>
    <p>
        {{ task_form.task.label }}<br>
        {{ task_form.task(class_="form-control") }}<br>
     <p>
        {{ task_form.hours.label }}<br>
        {{ task_form.hours(class_="form-control", type_="number") }}<br>
    </p>
     <p>
        {{ task_form.ID.label }}<br>
        {{ task_form.ID(class_="form-control", type_="number", placeholder="Numeric ID from the record below you wish to change") }}<br>
    </p>        
     <p>
        {{ task_form.submit(class_="btn btn-success btn-lg") }}
    </p>        
</form>

目前尚未进行验证,并且用户被发送到确认页面而不向数据库提交任何数据。任何帮助或指示将不胜感激!

1 个答案:

答案 0 :(得分:1)

表单操作指向错误的路径。