Route Handling in Python

From the Flask cheat sheet · Routing · verified Jul 2026

Route Handling

Define and organize routes

python
# Quick Reference
@app.route('/users/<int:user_id>')
def get_user(user_id):
    return {'id': user_id}

@app.route('/posts', methods=['GET', 'POST'])
def posts():
    if request.method == 'POST':
        return create_post()
    return get_posts()
💡 Use variable rules like <int:id> for type conversion
🔍 get_or_404() automatically returns 404 if not found
⚡ Specify methods parameter to handle multiple HTTP verbs
routingendpoints
Back to the full Flask cheat sheet