3

I need the request.path in my template tag. But the problem is, my django version is 1.5.1 and I dont have the TEMPLATE_CONTEXT_PROCESSORS, so there's no django.core.context_processors.request. Now, its giving me the error:

Exception Type: AttributeError
Exception Value:'str' object has no attribute 'path'
Exception Location:C:\Users\Nanyoo\web\pics\album\templatetags\active_tags.py in active, line 8

Is there any other way to get the desired path in the template?

views.py:

def home(request):
    photos = Photo.objects.all()
    return render(request,"index.html", {'photos':photos})

active_tags.py:

from django import template

register = template.Library()

@register.simple_tag
def active(request, pattern):
    import re
    if re.search(pattern, request.path):
        return 'active'
    return ''  
Robin
  • 5,366
  • 17
  • 57
  • 87

2 Answers2

1

Please pass the request object in the Context dictionary.

def home(request):
    photos = Photo.objects.all()
    return render(request,"index.html", {'photos':photos,'request':request})
0

If you want the request object to be always available in your templates, you can add to the default template context processors. See the answer here:

Where is template context processor in Django 1.5?

Basically, put this in your settings.py:

import django.conf.global_settings as DEFAULT_SETTINGS

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
    'django.core.context_processors.request',
)
Community
  • 1
  • 1
trpt4him
  • 1,646
  • 21
  • 34