Complete the code to import the CSRF protection extension in Flask.
from flask_wtf import [1]
The CSRFProtect class is imported from flask_wtf to enable CSRF protection in Flask apps.
Complete the code to initialize CSRF protection with the Flask app.
csrf = CSRFProtect()
app = Flask(__name__)
csrf.[1](app)The init_app method connects the CSRF protection to the Flask app instance.
Fix the error in the form class to include CSRF protection.
class MyForm([1]): name = StringField('Name') submit = SubmitField('Submit')
Forms that inherit from FlaskForm automatically include CSRF protection tokens.
Fill both blanks to create a dictionary comprehension that includes only fields with data and their CSRF token.
data = {field.name: field.data for field in form if field.[1] and field.name != '[2]'}We check field.data to include fields with data and exclude the csrf_token field.
Fill all three blanks to validate the form and handle CSRF errors in a Flask route.
from flask_wtf.csrf import [1] @app.route('/submit', methods=['POST']) def submit(): form = MyForm() if form.[2](): # process form data return 'Success' else: return [3]('CSRF token missing or invalid', 400)
CSRFError is imported to handle CSRF exceptions, validate_on_submit() checks form validity including CSRF, and abort sends an HTTP error response.