catching exceptions in zpt
I'm trying to do something like this in a zpt <meta tal:define="global target python:here.mysript(request.target) | nothing" /> however, if my request did not have ?target=blah I get an attributeError because of course the request has not parameter called target. I assumed that the pipe 'or nothing' would handle an equivalent except AttributeError block but it doesnt. how can I put in my zpt a way to catch if request has no attribute? -- David Bear College of Public Programs/ASU 411 N Central, Phoenix, AZ 85004
--On 10. März 2008 13:55:27 -0700 David Bear <david.bear@asu.edu> wrote:
I'm trying to do something like this in a zpt
<meta tal:define="global target python:here.mysript(request.target) | nothing" />
Please don't invent new syntax (you're mixing Python and TALES expressions). First 'global' variable definitions are usually not needed and considered bad. Use nested scopes instead (as we all do). Second, a tal:condition="request/target | nothing" allows you to to call the block only if target is present within the request. Third you might use getattr(request, 'target', 'default_target') in order to call myscript() with a reasonable default value as needed or use something like this: < meta tal:define="t request/target | string:default_target; target python: context.myscript(t); " /> -aj
participants (2)
-
Andreas Jung -
David Bear