django subdomain support for both resolve and reverse.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from django.core.urlresolvers import reverse
from django.conf import settings
from django.http import HttpResponseNotFound
from couponcn.middleware.threadinglocal import tl
import django

def _reverse(*args, **kwargs):
    url = reverse(*args, **kwargs)
    subdomain, path = url.split(':', 1)
    protocol = tl.request.is_secure() and 'https://' or 'http://'
    return '%s%s%s/%s' % (protocol, subdomain[1:], settings.BASE_DOMAIN, path)
django.core.urlresolvers.reverse = _reverse

class SubdomainMiddleware(object):
    def process_request(self, request):
        subdomain = request.get_host().split(settings.BASE_DOMAIN, 1)
        if len(subdomain)  < 2 or subdomain[0] == '':
            return HttpResponseNotFound()
        request.path_info = '/%s:%s' % (subdomain[0], request.path_info)


from threading import local
tl = local()
class ThreadingLocalMiddleware(object):
    def process_request(self, request):
        tl.request = request
        

More like this

  1. Resolve URLs to view name and args/kwargs by fahhem 3 years, 5 months ago
  2. Resolve URLs to view name by UloPe 5 years, 1 month ago
  3. Url filter middleware by limodou 7 years, 1 month ago
  4. Ensure submitted slugs do not conflict with existing resolvable URLs by ElfSternberg 4 years, 10 months ago
  5. Middleware to remove the WWW from the URL by pedrolima 6 years, 1 month ago

Comments

(Forgotten your password?)