Zope Book -> Chap 3 -> Interest Rate Example
Yesterday I managed to instal Python and Zope. Today I'm learning how to use Zope. I'm not 100% sure I understand the Zope model - but then I'm only on chapter 3 and I'm sure many of my questions will be answered later in the book. However, the interest rate example given in the book returns an error to me: "Zope Error Zope has encountered an error while publishing this resource. Error Type: KeyError Error Value: years" This appears to be quite a simple error where the form value "years" is not being seen by Zope. This error is thrown the first time "years" is referenced. That is: <span tal:content="years">2</span> This code appears in a Page Template. If I cut this code out, the page errors at the next form value (principal). Obviously I'm not "capturing" the form values submitted by the html form. However, The "Page Template" object doesn't give you a method to explicitly capture these form elements so I assume that this happens automagically. An incorrect assumption? Can anyone point me to where I'm going wrong? TIA Simon Forster ___________________________________________________ LDML Ltd, Office One, 16 Canham Road, London, W3 7SR, UK Tel: +44 (0)20 8749 7766 Fax: +44 (0)70 9230 5247 ___________________________________________________
On Thursday 05 June à 13:37, Simon Forster wrote:
Yesterday I managed to instal Python and Zope. Today I'm learning how to use Zope. I'm not 100% sure I understand the Zope model - but then I'm only on chapter 3 and I'm sure many of my questions will be answered later in the book. However, the interest rate example given in the book returns an error to me:
"Zope Error
Zope has encountered an error while publishing this resource.
Error Type: KeyError Error Value: years"
This appears to be quite a simple error where the form value "years" is not being seen by Zope. This error is thrown the first time "years" is referenced. That is:
<span tal:content="years">2</span>
This code appears in a Page Template.
If I cut this code out, the page errors at the next form value (principal).
Obviously I'm not "capturing" the form values submitted by the html form. However, The "Page Template" object doesn't give you a method to explicitly capture these form elements so I assume that this happens automagically. An incorrect assumption?
yes. You should access the submitted form value using the request variable : <span tal:content="request/years">2</span> or <span tal:content="request/form/years">2</span> (the first is a convenience method to transparently access request/form attributes) If you are not sure that the years field exists, you can use : <span tal:content="request/years | nothing">2</span> to avoid exception when it doesn't exist. -- Sylvain Thénault LOGILAB, Paris (France). http://www.logilab.com http://www.logilab.fr http://www.logilab.org
On Thursday, June 5, 2003, at 01:51 pm, Sylvain Thénault wrote:
<span tal:content="request/form/years">2</span>
Thank you for that. That fixes one problem but then: $<span tal:content="python: here.calculateCompoundingInterest(principal, interest_rate, periods, years)" >1.00</span> fails (as I would expect given your posting) but so does <span tal:content="python: here.calculateCompoundingInterest(request/form/principal, request/form/interest_rate, request/form/periods, request/form/years)" >1.00</span> with error: Error Type: NameError Error Value: global name 'form' is not defined while <span tal:content="python: here.calculateCompoundingInterest(request/principal, request/interest_rate, request/periods, request/years)" >1.00</span> fails with error Error Type: NameError Error Value: global name 'principal' is not defined Why? Simon Forster ___________________________________________________ LDML Ltd, Office One, 16 Canham Road, London, W3 7SR, UK Tel: +44 (0)20 8749 7766 Fax: +44 (0)70 9230 5247 ___________________________________________________
Here is the complete solution: <html> <body> <p>Your total balance (or debt) including compounded interest over <span tal:content="request/years">2</span> years is:</p> <span tal:define="principal request/principal; interest_rate request/interest_rate; periods request/periods; years request/years"> <p><b>$<span tal:content="python: here.calculateCompoundingInterest( principal, interest_rate, periods, years)"> 1.00 </span> </b> </p> </span> </body> </html> -----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org] On Behalf Of Simon Forster Sent: Thursday, June 05, 2003 9:05 AM To: Sylvain Thénault Cc: zope@zope.org Subject: Re: [Zope] Zope Book -> Chap 3 -> Interest Rate Example On Thursday, June 5, 2003, at 01:51 pm, Sylvain Thénault wrote:
<span tal:content="request/form/years">2</span>
Thank you for that. That fixes one problem but then: $<span tal:content="python: here.calculateCompoundingInterest(principal, interest_rate, periods, years)" >1.00</span> fails (as I would expect given your posting) but so does <span tal:content="python: here.calculateCompoundingInterest(request/form/principal, request/form/interest_rate, request/form/periods, request/form/years)" >1.00</span> with error: Error Type: NameError Error Value: global name 'form' is not defined while <span tal:content="python: here.calculateCompoundingInterest(request/principal, request/interest_rate, request/periods, request/years)" >1.00</span> fails with error Error Type: NameError Error Value: global name 'principal' is not defined Why? Simon Forster ___________________________________________________ LDML Ltd, Office One, 16 Canham Road, London, W3 7SR, UK Tel: +44 (0)20 8749 7766 Fax: +44 (0)70 9230 5247 ___________________________________________________ _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev ) --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.486 / Virus Database: 284 - Release Date: 5/29/2003 --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.486 / Virus Database: 284 - Release Date: 5/29/2003
On Thursday 05 June à 14:05, Simon Forster wrote:
On Thursday, June 5, 2003, at 01:51 pm, Sylvain Thénault wrote:
<span tal:content="request/form/years">2</span>
Thank you for that. That fixes one problem but then:
$<span tal:content="python: here.calculateCompoundingInterest(principal, interest_rate, periods, years)" >1.00</span>
fails (as I would expect given your posting) but so does
<span tal:content="python: here.calculateCompoundingInterest(request/form/principal, request/form/interest_rate, request/form/periods, request/form/years)" >1.00</span>
with error:
Error Type: NameError Error Value: global name 'form' is not defined
while
<span tal:content="python: here.calculateCompoundingInterest(request/principal, request/interest_rate, request/periods, request/years)" >1.00</span>
fails with error
Error Type: NameError Error Value: global name 'principal' is not defined
Why?
you can not access fields in the same way in the python: expression as in others TALES expressions. In a python expression, you can access form fields by using : request.form['years'] I suggest you to look at the page template reference [1] before the end of the book ;-) [1] http://www.zope.org/Documentation/Books/ZopeBook/current/AppendixC.stx -- Sylvain Thénault LOGILAB, Paris (France). http://www.logilab.com http://www.logilab.fr http://www.logilab.org
All of these problems are fixed in the online 2.6 edition of the Zope Book. (but note that it's now chapter 6): http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/BasicObject.stx -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's DARK EQUESTRIAN SADIST! (random hero from isometric.spaceninja.com)
On Thursday, June 5, 2003, at 10:44 am, Paul Winkler wrote:
All of these problems are fixed in the online 2.6 edition of the Zope Book. (but note that it's now chapter 6): http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/ BasicObject.stx
I'd just like to say THANK YOU to those involved with the Zope Book. The new version is far better than the old (but I'm not starting again from the beginning). Thank you Simon Forster ___________________________________________________ LDML Ltd, Office One, 16 Canham Road, London, W3 7SR, UK Tel: +44 (0)20 8749 7766 Fax: +44 (0)70 9230 5247 ___________________________________________________
participants (4)
-
Jamie White -
Paul Winkler -
Simon Forster -
Sylvain Thénault