Encapsulating Flask-WTF Form Validation Logic with Decorators
Don’t repeat yourself When using Flask-WTF, you often use the following code to validate the legitimacy of form data: from flask import Flask app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def index(): form = TestForm() # Check if valid if not form.validate_on_submit(): return 'err', 400 # Main logic For projects with many submission interfaces, you need to write the same logic under each route, resulting in a lot of code duplication. In Flask-Login, to set a route to be accessible only after logging in, you just need to add a @login_required decorator to the route without any extra code. Can we encapsulate the form validation logic with a decorator like Flask-Login? ...