Login

django subdomain support for both resolve and reverse.

Author:
puppy
Posted:
June 11, 2010
Language:
Python
Version:
1.2
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.