key error in templates

157 views
Skip to first unread message

Kenneth Gonsalves

unread,
Jun 16, 2010, 9:16:21 PM6/16/10
to Django users
hi,

in many models with an imagefield called photo, I have this code in the
template:

{% if p.photo %}
<img src="{% thumbnail p.photo 400x400 %}" />

{% endif %}

this has been working across many sites for years - and is working on a
particular site running Revision: 12453.

In my current project I am getting keyerror: photo where the photo does not
exist. This is running on latest trunk. The only difference between the two
sites is that in site where it is working, 'p' is a query set passed from the
view, whereas in the site where it is not working, the values are got from a
pickled file, so the code is like this:

{% if hand.0.player.photo %}hi{% endif %}

here hand.0.player is a Player model which has a field called photo. If the
photo does not exist I get a KeyError. The full error traceback is given
below:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/displayhandicap/
Django Version: 1.2.1 SVN-13354
Python Version: 2.6.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'djangogolf.web',
'sorl.thumbnail']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')


Template error:
In template /home/lawgon/djangogolf/templates/web/handicaplist.html, error at
line 20
Caught KeyError while rendering: photo
10 : <table>


11 : <tr>


12 : <th>Member </th>


13 : <th>Photo </th>


14 : <th>Club </th>


15 : <th>Handicap index </th>


16 : <th>Handicap for Ooty</th>


17 : <th>Handicap for Coimbatore</th>


18 : <th>Cut?</th>


19 : </tr>


20 : {% for hand in handlist.hlist %}


21 : <tr>


22 : <td><a
href="/scoringrecord/{{hand.0.id}}/">{{hand.0}}</a></td>


23 :


24 : <td>{% if hand.0.player.photo %}hi{% endif %}</td>


25 : <td>{{hand.0.player.homeclub.shortname}} </td>


26 : <td>{{hand.1}} </td>


27 : <td>{{hand.2}} </td>


28 : <td>{{hand.3}} </td>


29 : <td>{{hand.4}} </td>


30 : </tr>


Traceback:
File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py" in
get_response
100. response = callback(request, *callback_args,
**callback_kwargs)
File "/usr/lib/python2.6/site-packages/djangogolf/web/views.py" in
displayhandicap
1911. {'handlist':handlist,}))
File "/usr/lib/python2.6/site-packages/django/shortcuts/__init__.py" in
render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs),
**httpresponse_kwargs)
File "/usr/lib/python2.6/site-packages/django/template/loader.py" in
render_to_string
186. return t.render(context_instance)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in render
173. return self._render(context)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in _render
167. return self.nodelist.render(context)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in render
796. bits.append(self.render_node(node, context))
File "/usr/lib/python2.6/site-packages/django/template/debug.py" in
render_node
72. result = node.render(context)
File "/usr/lib/python2.6/site-packages/django/template/loader_tags.py" in
render
125. return compiled_parent._render(context)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in _render
167. return self.nodelist.render(context)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in render
796. bits.append(self.render_node(node, context))
File "/usr/lib/python2.6/site-packages/django/template/debug.py" in
render_node
72. result = node.render(context)
File "/usr/lib/python2.6/site-packages/django/template/loader_tags.py" in
render
62. result = block.nodelist.render(context)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in render
796. bits.append(self.render_node(node, context))
File "/usr/lib/python2.6/site-packages/django/template/debug.py" in
render_node
72. result = node.render(context)
File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py" in
render
251. return self.nodelist_true.render(context)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in render
796. bits.append(self.render_node(node, context))
File "/usr/lib/python2.6/site-packages/django/template/debug.py" in
render_node
72. result = node.render(context)
File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py" in
render
167. nodelist.append(node.render(context))
File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py" in
render
246. var = self.var.eval(context)
File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py" in eval
751. return self.value.resolve(context, ignore_failures=True)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in resolve
553. obj = self.var.resolve(context)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in resolve
696. value = self._resolve_lookup(context)
File "/usr/lib/python2.6/site-packages/django/template/__init__.py" in
_resolve_lookup
725. current = getattr(current, bit)
File "/usr/lib/python2.6/site-packages/django/db/models/fields/files.py" in
__get__
175. file = instance.__dict__[self.field.name]

Exception Type: TemplateSyntaxError at /displayhandicap/
Exception Value: Caught KeyError while rendering: photo
--
Regards
Kenneth Gonsalves
Senior Associate
NRC-FOSS at AU-KBC

Kenneth Gonsalves

unread,
Jun 16, 2010, 9:33:27 PM6/16/10
to django...@googlegroups.com
On Thursday 17 June 2010 06:46:21 Kenneth Gonsalves wrote:
> in many models with an imagefield called photo, I have this code in the
> template:
>
> {% if p.photo %}
> <img src="{% thumbnail p.photo 400x400 %}" />
>
> {% endif %}
>
> this has been working across many sites for years - and is working on a
> particular site running Revision: 12453.
>
> In my current project I am getting keyerror: photo where the photo does
> not exist. This is running on latest trunk. The only difference between
> the two sites is that in site where it is working, 'p' is a query set
> passed from the view, whereas in the site where it is not working, the
> values are got from a pickled file, so the code is like this:
>
> {% if hand.0.player.photo %}hi{% endif %}
>

I have now confirmed that this KeyError also comes in revision 12453 - so it is
a problem with my code

Kenneth Gonsalves

unread,
Jun 17, 2010, 12:28:47 AM6/17/10
to django...@googlegroups.com
On Thursday 17 June 2010 06:46:21 Kenneth Gonsalves wrote:
> In my current project I am getting keyerror: photo where the photo does
> not exist. This is running on latest trunk. The only difference between
> the two sites is that in site where it is working, 'p' is a query set
> passed from the view, whereas in the site where it is not working, the
> values are got from a pickled file, so the code is like this:
>
> {% if hand.0.player.photo %}hi{% endif %}
>

solved - what had happened was that when I pickled the results, the model in
question did not have a 'photo' field which I added subsequently to pickling.
Once I repickled, the problem was solved

Venkatraman S

unread,
Jun 17, 2010, 12:32:20 AM6/17/10
to django...@googlegroups.com


On Thu, Jun 17, 2010 at 9:58 AM, Kenneth Gonsalves <law...@au-kbc.org> wrote:
solved - what had happened was that when I pickled the results, the model in
question did not have a 'photo' field which I added subsequently to pickling.
Once I repickled, the problem was solved


Totally not related to your Q, but if you are using photos - try using django-photologue. Its awsum!

-V-
http://twitter.com/venkasub

Kenneth Gonsalves

unread,
Jun 17, 2010, 12:41:14 AM6/17/10
to django...@googlegroups.com
On Thursday 17 June 2010 10:02:20 Venkatraman S wrote:
> > Once I repickled, the problem was solved
>
> Totally not related to your Q, but if you are using photos - try using
> django-photologue. Its awsum!
>

was awesome - I do not think it is maintained now, I tried it with trunk and
gave up after 5 minutes - I have my own gallery app which is enough for my
purposes

Evan Reiser

unread,
Nov 12, 2011, 4:10:53 PM11/12/11
to django...@googlegroups.com
I had the same thing happen to me and this was the only post I found on the internet that mentions it.

Note to other people who have this problem: 

Be careful passing instances of model objects into celery tasks since they get pickled and your FileField fields will probably be removed via pickling.

The reason this is a problem is because of the way ImageField (and I assume FileFields) work.  When there is no File object associated with the FileField (its set to None) and you call instance.filefieldname on the instance it will return "None" and be == to "None" even though the field exists (without a file).  Since the field 'appears' to be not set (it evaluates to None) i don't think pickle is adding it to the serialized version of the instance, so when you de-serialize (in the celery task, or wherever) the field does not exist  
Reply all
Reply to author
Forward
0 new messages