Add site info to request context

1
2
3
4
5
6
7
8
9
from django.contrib.sites.models import Site, RequestSite

def site(request):
    site_info = {'protocol': request.is_secure() and 'https' or 'http'}
    if Site._meta.installed:
        site_info['domain'] = Site.objects.get_current().domain
    else:
        site_info['domain'] = RequestSite(request).domain
    return site_info

More like this

  1. Simple E-mail registration by bthomas 4 years, 7 months ago
  2. Template context processor to make the settings.py values available in templates by tamizhgeek 2 years, 1 month ago
  3. template filter to include protocol and domain in absolute urls by czer 11 months, 2 weeks ago
  4. Add URL Segments to Templates by epicserve 4 years, 8 months ago
  5. Mobile browser detection middleware by pavl 3 years, 1 month ago

Comments

Archatas (on November 17, 2008):

The absolute url might also include a specific port. So that should be also included into the snippet.

#

(Forgotten your password?)