Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 June 17, 2010 02:15:17

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

key error in templates


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:

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__

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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 June 17, 2010 02:32:23

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

key error in templates


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
--
Regards
Kenneth Gonsalves
Senior Associate
NRC-FOSS at AU-KBC

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 19th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support