Login

django subdomain support for both resolve and reverse.

Author:
puppy
Posted:
June 11, 2010
Language:
Python
Version:
1.2
Tags:
url reverse resolve subdomain
Score:
1 (after 1 ratings)

Add these two middleware to the top of MIDDLEWARE_CLASSES. Add BASE_DOMAIN to your setting file : BASE_DOMAIN = '.13.com'.

your root urlconf may like this: urlpatterns = patterns('', url(r'^www:(?P<id>[0-9]+)/$', 'couponcn.store.views.site_index', name='site_index'), url(r'^news:abc/def/$', 'couponcn.store.views.site_index', name='site_index2'), )

then {% url site_index id=4 %}<br /> {% url site_index2 %}

in your template or the reverse function will work out urls like this: http://www.13.com/4/ http://news.13.com/abc/def/

 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 4 years, 4 months ago
  2. Resolve URLs to view name by UloPe 6 years ago
  3. Url filter middleware by limodou 8 years ago
  4. Ensure submitted slugs do not conflict with existing resolvable URLs by ElfSternberg 5 years, 9 months ago
  5. Middleware to remove the WWW from the URL by pedrolima 7 years ago

Comments

Please login first before commenting.