Here's the code

@app.route('/account/<user_id>', methods = ["GET", "POST"])
@login_required
def user(user_id):
    user = User.query.filter_by(id = user_id).first()
 ...
return render_template('template.html')

If someone manually enters
htp://localhost/user/aaa for example, it throws an error. How can I check that the user id is an int before it throws an error? On the production server, it will probably throw a server error, but it would be better just to throw a 403 forbidden error.

Thank you!

有帮助吗?

解决方案

Use int converter:

@app.route('/account/<int:user_id>', methods = ["GET", "POST"])
@login_required
def user(user_id):
    user = User.query.filter_by(id = user_id).first()
    ...
    return render_template('template.html')

Also see documentation: http://werkzeug.pocoo.org/docs/routing/#builtin-converters.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top