jedierikb
jedierikb

Reputation: 13079

context KeyError in django templateTag

My app's templatetag code is throwing a KeyError for a missing key (page) in the context variable. In my template, I do not refer to context variables with context.variableKeyName, I just refer to variableKeyName (e.g. {% if is_paginated %}). And in my template, I can refer to the key page without any exceptions.

How should I get the context with the keys it needs into my templatetag?


Here are the details:

I am using django-profiles to return a list of some profiles:

url(r'^profiles/$', 'profiles.views.profile_list', 
kwargs={ 'paginate_by':10 }, 
name='profiles_profile_detail'),

which calls this bit of code here: https://bitbucket.org/ubernostrum/django-profiles..

In my template, I test {% if is_paginated %} before I call a templatetag:

{% if is_paginated %}{% load paginator %}{% paginator 3 %}{% endif %}

( I am using a templatetag inspired from http://www.tummy.com/.../django-pagination/ updated for django 1.3 http://djangosnippets.org/snippets/2680/ )

But this leads to the KeyError for 'paged'.

Upvotes: 2

Views: 1082

Answers (2)

Yuri Duarte
Yuri Duarte

Reputation: 1

the right way to code is:

{% paginator v 3 %}

v - variable that contains the db items

Upvotes: 0

Simeon Visser
Simeon Visser

Reputation: 122326

The documentation (comments in the class) of http://djangosnippets.org/snippets/2680/ says:

Required context variables: paged: The Paginator.page() instance.

It is also used in the template tag:

paged = context['paged']

You need to provide this context variable for this template tag to work. I think your best bet is to copy the code of profiles.views.profile_list view and add this context variable. It's unfortunately still a function-based view - otherwise extending it would have been a lot cleanier and easier.

Upvotes: 1

Related Questions