Preferred Domain decorator function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from django.http import HttpResponse,  HttpResponseRedirect
from django.contrib.sites.models import Site

def prefered_domain(fn):
    """
    Allows for a preferred domain name to be enforced on a per_view basis
    if no domain is found, it fails silently.
    """
    def _prefered_domain(request, site_id=1):
        try:
            site = Site.objects.get(id=site_id)
            if request.META['HTTP_HOST'] not in site.domain:
                return HttpResponseRedirect('http://%s%s' % (site.domain, request.META['PATH_INFO']) )
        except Site.DoesNotExist:
            pass
        return fn(request)
    return _prefered_domain

More like this

  1. Decorator cache handler per view by guicifuentes 5 years, 7 months ago
  2. Cache Any Function by jeffwheeler 7 years, 1 month ago
  3. Header view decorators by ydm 1 year, 1 month ago
  4. decorator to add author to extra_fields in snippets 635 by gehel 5 years, 11 months ago
  5. Ignore HTTP Accept-Language headers by fonso 6 years, 11 months ago

Comments

(Forgotten your password?)