简体   繁体   中英

Flask WTForms validate_on_submit won't execute

The flask form below renders just fine, though it accepts any (or no) input and always redirects to the target URL on submit. Looking for solutions this week turned up similar cases, though most of these were related to missing CSRF tokens ( example ). There's definitely a CSRF token via form.hidden_tag(), and when the Werkzeug debugger is open, both form.validate() and form.validate_on_submit() return output. However, neither statement produces output if included in the source code as print statements.

Here are the forms in the model:

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()])

Here are the forms in views:

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

And here is the template:

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

Currently no validation occurs, and the user is sent to the confirmation page without committing any data to the database. Any help or pointers would be greatly appreciated!

表单动作指向错误的路线。

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