Form rendering, validation, and CSRF protection for Flask with WTForms.
72
Build a Flask application with a user registration form that properly integrates CSRF protection at both the application and form levels.
Create a Flask web application with the following components:
A user registration form with the following fields:
CSRF protection must be enabled globally for the entire application using the appropriate extension.
The registration form must also include form-level CSRF protection.
Create a route /register that:
Create a route /health that is exempt from CSRF protection and returns "OK" for health checks.
The application should properly handle CSRF token validation without performing redundant checks when both global and form-level protection are active.
/register, the response contains a CSRF token field @test/register, the response is "Registration successful" @test/register, the response has a 400 status code @test/health, the response is "OK" @test/health, the response is "OK" (no CSRF check) @test@generates
# Flask application setup
from flask import Flask
app = Flask(__name__)
app.config['SECRET_KEY'] = 'test-secret-key-12345'
# Registration route
@app.route('/register', methods=['GET', 'POST'])
def register():
"""Handle user registration with CSRF-protected form."""
pass
# Health check route (CSRF exempt)
@app.route('/health', methods=['GET', 'POST'])
def health():
"""Health check endpoint without CSRF protection."""
passProvides the web framework for building the application.
Provides form handling and CSRF protection for Flask applications.
Provides form field definitions and validation support.
Install with Tessl CLI
npx tessl i tessl/pypi-flask-wtfevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10