Python > Web Development with Python > Flask > Blueprints
Dynamic Blueprints with Subdomains
This snippet shows how to use Blueprints to handle different subdomains for various parts of your application. This is helpful for creating multi-tenant applications or separating functionalities based on subdomains.
Subdomain Blueprints Explanation
This example demonstrates how to create and register a Blueprint that operates under a specific subdomain. This is useful for separating different parts of your application into distinct subdomains, such as admin.example.com
or api.example.com
.
Creating a Subdomain Blueprint
The crucial part here is the subdomain='admin'
argument when creating the Blueprint. This tells Flask that routes defined in this Blueprint should be accessed under the admin
subdomain.
from flask import Blueprint, render_template
# Create a Blueprint for the 'admin' subdomain
admin_bp = Blueprint('admin', __name__, subdomain='admin')
@admin_bp.route('/')
def admin_index():
return 'Admin Dashboard'
Registering the Subdomain Blueprint
To enable subdomain routing, you must set the Important: You will also need to configure your hosts file (or DNS settings if deploying to a live server) to point the subdomain (e.g., SERVER_NAME
configuration variable in your Flask app. This tells Flask the base domain that it should use for subdomain matching. Registering the Blueprint is the same as before, using app.register_blueprint()
.admin.example.com
) to your server's IP address.
from flask import Flask
app = Flask(__name__)
app.config['SERVER_NAME'] = 'example.com:5000' # Important for subdomain routing
# Import the Blueprint
from .admin import admin_bp
# Register the Blueprint
app.register_blueprint(admin_bp)
@app.route('/')
def index():
return 'Main Website'
if __name__ == '__main__':
app.run(debug=True)
Accessing the Subdomain
To access the admin dashboard, you would need to navigate to admin.example.com:5000
in your browser (assuming your Flask app is running on port 5000 and you've configured your hosts file or DNS correctly). Accessing example.com:5000
would display 'Main Website'.
Real-Life Use Case Section
Subdomain Blueprints are ideal for:tenant1.myapp.com
, tenant2.myapp.com
).api.v1.example.com
, api.v2.example.com
).admin.example.com
).
Best Practices
SERVER_NAME
: Always set the SERVER_NAME
configuration variable when using subdomain routing.
Interview Tip
When discussing Blueprints in an interview, be sure to mention the benefits of using subdomains to structure complex applications and how it improves maintainability and scalability. Highlight the importance of the SERVER_NAME
configuration and DNS management.
Alternatives
Alternatives to subdomain routing include:/admin
, /api
). This is simpler but can become less organized as the application grows.
Pros
Cons
FAQ
-
Why do I need to set
SERVER_NAME
?
TheSERVER_NAME
configuration variable tells Flask the base domain to use for subdomain matching. Without it, Flask won't be able to determine which Blueprint to route requests to based on the subdomain. -
How do I handle different ports for different subdomains?
You typically wouldn't handle different ports directly. You would use a reverse proxy (e.g., Nginx or Apache) to route traffic to the appropriate Flask application or Blueprint based on the subdomain, all on port 80 or 443.