dotty dotty - 6 months ago 12
Python Question

wildcard subdomains in a django project

has anyone ever used wildcard subdomains in their application? I need to come up with a way to 'localise' my application. When i say localise i mean anyone who goes to
will be sent to a view which looks through a database and search for new items in ny. Obviously we could replace NY with any state.

Any tips would be great



I would do it using a middleware, eg.:

class StateCodeMiddleware(object):

   def process_request(self, request):
        bits = request.META['HTTP_HOST'].split('.')
        if len(bits) == 3 and len(bits[0]) == 2:
            request.state_code = bits[0]
            request.state_code = None
            # Or a redirect to the default state.

And then in any of your views, you can just check request.state_code and fetch new items only for that state.

Edit: For development, the best method is to setup a local DNS server. Eg. dnsmasq is very easy to configure:

address=/.dev/ # in dnsmasq.conf

This makes *.dev point to localhost. You'll also have to configure your system to use the local DNS server (on UNIX systems you do this by placing nameserver into /etc/resolve.conf).

Alternatively, you can list all the domain names in your /etc/hosts if it is a finite set: ny.localhost, az.localhost  # and so on