简体   繁体   中英

Flask-WTForms error when specifying validator of maximum length

I have had some trouble getting my maximum length validator for a search bar to work with my flask app. I am currently getting an error: TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement. TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

Here is my forms.py:

    class SearchForm(FlaskForm):
    query = StringField('query', validators=[DataRequired(), Length(max=20)])
    submit = SubmitField('🔍')

and my route in routes.py:

@app.route('/gsearch', methods=['POST'])
def gsearch():
    conn = sqlite3.connect("retro_games.db")
    cur = conn.cursor()
    form = SearchForm()
    if form.validate_on_submit():
        cur.execute("SELECT * FROM Games WHERE name LIKE ?",
                    ("%"+form.query.data+"%",))
        game = cur.fetchall()
        return render_template('gsearch.html', title='Search', game=game)

Thanks for the help:)

You should handle when form is not valid,

def gsearch():
    conn = sqlite3.connect("retro_games.db")
    cur = conn.cursor()
    form = SearchForm()
    if form.validate_on_submit():
        cur.execute("SELECT * FROM Games WHERE name LIKE ?",
                    ("%"+form.query.data+"%",))
        game = cur.fetchall()
        return render_template('gsearch.html', title='Search', game=game)
    # if form is not vaild, handle the logic here
    return {"error": "game not found"}

for more info check FLASK-WTF Validating Forms

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