Tricky usage of string to integer conversion
I am trying to do the following: <dtml-let x:int="REQUEST.get('prod_config')" item="_[REQUEST.get('prod_id')]" price="item.price_list[x]"> <INPUT type="hidden" name="chargetotal" value="<dtml-var price>"> </dtml-let> The prod_config variable is a number (returned as a string) from a SELECT list. The prod_id variable is the id of an object in the same folder as this method that has a price_list attribute. price_list is a list of strings representing prices. This results in a "Name Error" where Zope cannot find "x". If I change line 3 to this: price="item.price_list['x']"> it fixes the Name Error, but then I get a "Sequence Index must be an integer" error on price_list['x']. I would have thought the use of "x:int" in my <dtml-let> would take care of this issue. Seems I can't win! Does anyone know the correct way to do this lookup? Best Regards, Brian Holdsworth
----- Original Message ----- From: Brian K. Holdsworth <bhold@awod.com>
I am trying to do the following:
<dtml-let x:int="REQUEST.get('prod_config')" item="_[REQUEST.get('prod_id')]" price="item.price_list[x]"> <INPUT type="hidden" name="chargetotal" value="<dtml-var price>"> </dtml-let>
"dtml-let" doesn't understand type annotations, so you are setting a variable called 'x:int', which isn't what you want. You need to do the integer conversion in Python, like so: <dtml-let x="_.int(REQUEST.get('prod_config')" ...> See the DTML reference guide at zope.org for more useful methods of "_". Cheers, Evan @ digicool & 4-am
"Brian K. Holdsworth" schrieb:
I am trying to do the following:
<dtml-let x:int="REQUEST.get('prod_config')" item="_[REQUEST.get('prod_id')]" price="item.price_list[x]"> <INPUT type="hidden" name="chargetotal" value="<dtml-var price>"> </dtml-let>
The prod_config variable is a number (returned as a string) from a SELECT list. The prod_id variable is the id of an object in the same folder as this method that has a price_list attribute. price_list is a list of strings representing prices.
This results in a "Name Error" where Zope cannot find "x". If I change line 3 to this:
price="item.price_list['x']">
it fixes the Name Error, but then I get a "Sequence Index must be an integer" error on price_list['x']. I would have thought the use of "x:int" in my <dtml-let> would take care of this issue. Seems I can't win!
Does anyone know the correct way to do this lookup?
I think it must be converted by the atoi method of the string module: so either: <dtml-let x="_.string.atoi(REQUEST.get('prod_config'))" [rest of your code] or: <dtml-let x="REQUEST.get('prod_config')" item="_[REQUEST.get('prod_id')]" price="item.price_list[_.string.atoi(x)]"> hth, Thomas
Other people answered your conversion question, I just wanted to point out that the ':int' notation is meant for use in qualifying HTTP POSTED data, like that coming from a form. For example: <form> <input name="bob:int"> </form> This will cause ZPublisher to try and turn the value of this element into an integer. This is the only place the ':int' notation makes any sense. In your example, you have confused this notation with that of the let tag. -Michel "Brian K. Holdsworth" wrote:
I am trying to do the following:
<dtml-let x:int="REQUEST.get('prod_config')" item="_[REQUEST.get('prod_id')]" price="item.price_list[x]"> <INPUT type="hidden" name="chargetotal" value="<dtml-var price>"> </dtml-let>
The prod_config variable is a number (returned as a string) from a SELECT list. The prod_id variable is the id of an object in the same folder as this method that has a price_list attribute. price_list is a list of strings representing prices.
This results in a "Name Error" where Zope cannot find "x". If I change line 3 to this:
price="item.price_list['x']">
it fixes the Name Error, but then I get a "Sequence Index must be an integer" error on price_list['x']. I would have thought the use of "x:int" in my <dtml-let> would take care of this issue. Seems I can't win!
Does anyone know the correct way to do this lookup?
Best Regards, Brian Holdsworth
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
participants (4)
-
Brian K. Holdsworth -
Evan Simpson -
Michel Pelletier -
Thomas Weiner