Testing Django apps using localhost subdomains
This turned out to be quite a bit easier than I'd imagined. Here are the things I did:
-
I saved Dave Fowler's subdomain middleware as
middleware.pyin my project directory:class SubdomainMiddleware: def process_request(self, request): '''Parse out the subdomain from the request''' request.subdomain = None host = request.META.get('HTTP_HOST', '') host_s = host.replace('www.', '').split('.') if len(host_s) > 2: request.subdomain = ''.join(host_s[:-2]) -
I added this to my project's
MIDDLEWARE_CLASSES:MIDDLEWARE_CLASSES = ( ..., 'middleware.SubdomainMiddleware', ) -
I edited my
/etc/hostsfile as per Dave's suggestion:127.0.0.1 test.com 127.0.0.1 blog.test.com 127.0.0.1 search.test.com
Initially I replaced
test.comwith the site's domain name, but I decided that it's useful to be able to access both the live site and the test site without editing the/etc/hostsfile.At this point I expected everything to work as advertised. Instead, I got this:

That would depend on one's definition of "works". I wanted my Django site to appear, which required a very simple tweak…
-
I added the port number to the address:
http://test.com:8000/
This actually worked. :)
Possibly related posts
- Django syntax highlighting for Coda
- Empty collections are valid cache data
- Show full directory path in Finder window title bar
- Changing keyboard shortcuts in Mac OS X
- Multisession CD burning in Snow Leopard