Middleware for contrib.sites to get the site matching the current host name
|Reported by:||Owned by:||Adrian Holovaty|
|Cc:||ferringb@…, xphuture@…, joesaccount@…, flosch@…||Triage Stage:||Design decision needed|
|Has patch:||yes||Needs documentation:||yes|
|Needs tests:||yes||Patch needs improvement:||no|
Scenario: A (contrived) blog host (or any other sort of user-content-based provider) wants to easily let users sign up for myblogname.example.com rather than sticking them at example.com/myblogname/ or worse.
SetSiteFromHost is a very simple middleware option perfect for
django.contrib.sites.middleware that changes
django.conf.settings.SITE_ID to the id of the Site with the same site.domain as
request.META['HTTP_HOST'] if such a site exists (otherwise it leaves it as set in settings, which is useful for a default site (for errors or whatnot)). This allows a wildcard
VirtualHost setup to be easily created for a django website whose applications support
django.contrib.sites. To add a new supported site, ready for content, the registration form should just need to create a new Site object. No mucking with generating
VirtualHost configs, settings files, or even burdening existing views with the overhead of site discovery. The trade-off here is that an additional query is run by the middleware than for a site that has an explicit SITE_ID that doesn't vary on the host.
I apologize for not having explicit tests or a documentation patch, but I wanted to get a feel for the comments/interest in adding this to Django SVN before seeking out the test exmaples and best practices for Middleware. I thought this was an almost obvious middleware for
django.contrib.sites to have and am almost surprised it doesn't. One improvement to my simple first attempt that is attached is that it should be easy enough to add support to cache the
request.META['HTTP_HOST'] -> site.id look up for subsequent page views in the hopes that might even further mitigate the performance trade-off of using this middleware over an explicit and unchanging SITE_ID.
Change History (18)
comment:1 Changed 10 years ago by
|Triage Stage:||Unreviewed → Design decision needed|
comment:10 Changed 9 years ago by
|Status:||closed → reopened|
|Summary:||Proposed additon to django.contrib.sites: middleware.SetSiteFromHost → Middleware for contrib.sites to get the site matching the current host name|