Login

location context_processor

Author:
gsf0
Posted:
August 14, 2009
Language:
Python
Version:
1.1
Score:
1 (after 1 ratings)

A simple context_processor to include location info. Useful for permalinks, site name references, and navigation bars. For example:

{% if location.path|match:"/$" %} class="current"{% endif %}

See also my match filter.

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

def location(request):
    location = {}

    current_site = Site.objects.get_current()
    location['site'] = current_site
    
    script_name = request.META['SCRIPT_NAME']
    location['script_name'] = script_name
    
    path = request.META['PATH_INFO']
    location['path'] = path
    
    url = 'http://%s%s%s' % (current_site, script_name, path)
    location['url'] = url
    
    return {'location': location}

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 9 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 3 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
  5. Help text hyperlinks by sa2812 1 year, 5 months ago

Comments

Please login first before commenting.