Performance: Why form handling matters
MEDIUM IMPACT
Form handling affects page responsiveness and server load during user input submission.
from flask import Flask, request from wtforms import Form, StringField, PasswordField, validators app = Flask(__name__) class LoginForm(Form): email = StringField('Email', [validators.Email()]) password = PasswordField('Password', [validators.Length(min=8)]) @app.route('/submit', methods=['POST']) def submit(): form = LoginForm(request.form) if not form.validate(): return 'Invalid input', 400 # Process data return 'Success', 200
from flask import Flask, request app = Flask(__name__) @app.route('/submit', methods=['POST']) def submit(): data = request.form # Validate all fields manually if not data.get('email') or '@' not in data.get('email'): return 'Invalid email', 400 if not data.get('password') or len(data.get('password')) < 8: return 'Password too short', 400 # Process data return 'Success', 200
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full page reload on submit | High (reloads entire DOM) | Multiple reflows | High paint cost | [X] Bad |
| AJAX form submission | Low (partial DOM update) | Single reflow | Low paint cost | [OK] Good |
| Manual server validation | N/A | N/A | Blocks server response | [X] Bad |
| WTForms validation | N/A | N/A | Faster server response | [OK] Good |