Pompeyo
Pompeyo

Reputation: 1469

Django's inclusion_tag not find context['request']

Using inclusion_tag I am trying to create a header with user links depending on the login status (eg. If user logged; display username and log out link, otherwise log in and sign up link). To do that, I created my own Login App so I am not using any Django's MIDDLEWARE_CLASSES

What am I missing? Any help would be appreciated, thanks!

keyError

Exception Type: KeyError
Exception Value: 'request'

templatetags/login_tags.py

from django import template
from login.models import Login
from test.views import read_secure_cookie

def login_finder(context):
    user = {} 
    active_user = read_secure_cookie(context['request'], 'user_id')
    id = active_user[0]
    try:
        login = Login.objects.filter(id=id).get()
    except Login.DoesNotExist:
        return None

    user['user'] = login.username
    return user

register = template.Library()
register.inclusion_tag('login/header.html', takes_context=True)(login_finder)

views.py

def frontpage(request):
    params = {}
    return render_to_response('frontpage.html', 
                              params, 
                              context_instance=RequestContext(request))

template/login/frontpage.html

{% extends "base.html" %}
{% load login_tags %}

{% block title %}{{ block.super }}{% endblock %}

{% login_finder %}    

Upvotes: 0

Views: 336

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599630

The request context processor is not active by default. You need to add it to TEMPLATE_CONTEXT_PROCESSORS in settings.py yourself.

Upvotes: 1

Related Questions