catch exceptions in templates?

158 views
Skip to first unread message

aliane abdelouahab

unread,
Oct 5, 2012, 1:58:05 PM10/5/12
to Tornado Web Server
hi, i get errors when trying to catch exceptions:

ParseError: except block cannot be attached to for block

what i try to do, is to detect if a product in a user cart has been
deleted by detecting the image in gridfs because i've duplicated the
information of this product to make it fast for the database.

here is what i did:

class AchatsHandler(BaseHandler):
@tornado.web.authenticated
def get(self):
user = self.get_secure_cookie("mechtari")
info = tornado.escape.json_decode(user)
email = info["personnel"]["email"]
try:
produits = self.db.users.find({"personnel.email":email},
{"produit_down":1, "_id":0}).distinct("produit_down")
renderer = self.fs
error = gridfs.NoFile
except (errors.AutoReconnect, errors.ConnectionFailure):
self.redirect("/error")
self.render("achats.html", produits=produits,
renderer=renderer, error=error)


{% if produits==[] %}
there is no products
{% else %}
{% try %}
{% for produit in produits %}
information about the produt
{% except error %}
this product dont exist
{% end %}
{% end %}
{% end %}

and the error:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\web.py", line 1064, in _execute
getattr(self, self.request.method.lower())(*args, **kwargs)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\web.py", line 1846, in wrapper
return method(self, *args, **kwargs)
File "G:\Mon projet\sog-emouk\handlers.py", line 486, in get
self.render("achats.html", produits=produits,
renderer=renderer, error=error)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\web.py", line 515, in render
html = self.render_string(template_name, **kwargs)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\web.py", line 619, in render_string
t = loader.load(template_name)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\template.py", line 326, in load
self.templates[name] = self._create_template(name)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\template.py", line 358, in _create_template
template = Template(f.read(), name=name, loader=self)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\template.py", line 220, in __init__
self.file = _File(self, _parse(reader, self))
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\template.py", line 831, in _parse
block_body = _parse(reader, template, operator, in_loop)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\template.py", line 831, in _parse
block_body = _parse(reader, template, operator, in_loop)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\template.py", line 825, in _parse
block_body = _parse(reader, template, operator, operator)
File "C:\Python27\lib\site-packages\tornado-2.4.post1-py2.7.egg
\tornado\template.py", line 777, in _parse
raise ParseError("%s block cannot be attached to %s block" %
(operator, in_block))
ParseError: except block cannot be attached to for block

[E 121005 18:14:19 web:1485] 500 GET /achats (::1) 12.00ms


but if i do this:

{% try %}
{% if produits==[] %}
..
{% else %}
{% for produit in produits %}
..
{% end %}
{% end %}
{% except error %}
<h2>No file!</h2>
{% end %}

i dont get the result i want; dot get "no file!" and the product is
shown!

Russ Weeks

unread,
Oct 5, 2012, 5:26:40 PM10/5/12
to python-...@googlegroups.com
Hi, Aliane,

Move the 'try' into the 'for' loop:

{% for produit in produits %}
{% try %}
[...]

Or, move the 'except' out of the 'for' loop.  (This will compile but the behaviour will not quite be how you want)
-Russ

aliane abdelouahab

unread,
Oct 5, 2012, 5:29:15 PM10/5/12
to Tornado Web Server
thank you!
yes, sadly what i dident understood, is why not making "endif, endfor,
endwhile...." so like that tornado will know where he is?
i'll try your solution and feedback .
thank you again.

On 5 oct, 22:26, Russ Weeks <rwe...@newbrightidea.com> wrote:
> Hi, Aliane,
>
> Move the 'try' into the 'for' loop:
>
> {% for produit in produits %}
> {% try %}
> [...]
>
> Or, move the 'except' out of the 'for' loop.  (This will compile but the
> behaviour will not quite be how you want)
> -Russ
>
> On Fri, Oct 5, 2012 at 10:58 AM, aliane abdelouahab <alabdeloua...@gmail.com

aliane abdelouahab

unread,
Oct 6, 2012, 10:56:00 AM10/6/12
to Tornado Web Server
sadly this dident work, the solution is to delete both product, from
uploader and user's cart.

On 5 oct, 22:26, Russ Weeks <rwe...@newbrightidea.com> wrote:
> Hi, Aliane,
>
> Move the 'try' into the 'for' loop:
>
> {% for produit in produits %}
> {% try %}
> [...]
>
> Or, move the 'except' out of the 'for' loop.  (This will compile but the
> behaviour will not quite be how you want)
> -Russ
>
> On Fri, Oct 5, 2012 at 10:58 AM, aliane abdelouahab <alabdeloua...@gmail.com
Reply all
Reply to author
Forward
0 new messages