Can anyone please help with this, I'm new to zope and trying to write a simple python script to handle some logic which will then call a dtml method. I'm calling the method as such: context.scReport() The error I'm getting back is Error Type: KeyError Error Value: standard_html_header standard_html_header is being called from the scReport method which suggests the scReport method is being invoked but why are we getting an error?? Any suggestions, TIA Shaneo
I ran into this awhile back. Try this: context.scReport(None, _) Interestingly enough, I also learned that you can pass things into a dtml-method and they become available to use internally. For example with the following call: context.scReport(None, _, MsgToUser='Hello') Inside your dtml-method you could do a <dtml-var MsgToUser> This is very usefull to passing stuff around without cluttering up your REQUEST namespace. Another little trick I learned. Want to find an object via acquisition and then call it? I found that the following worked in a python script: methodToCall = _.getitem('methodName') # Find object via acquisition and get handle on it. print methodToCall(None, _) # Call the method and print the results. This is really usefull if you generate the method name as I do depending on the request. ie methodToCall = _.getitem(someStrVar + 'form') will concatenate the two strings and then look for that object in the namespace. If you do this kind of lookups, you should probably also check for existence before calling it, ie _.has_key(someStrVar + 'form'). Hope that helps. -Chris ------------------------------ Chris Kratz chris.kratz@vistashare.com ----- Original Message ----- From: Shane O'Sullivan To: zope@zope.org Sent: Tuesday, October 30, 2001 11:13 AM Subject: [Zope] calling dtmlMethod from python Can anyone please help with this, I'm new to zope and trying to write a simple python script to handle some logic which will then call a dtml method. I'm calling the method as such: context.scReport() The error I'm getting back is Error Type: KeyError Error Value: standard_html_header standard_html_header is being called from the scReport method which suggests the scReport method is being invoked but why are we getting an error?? Any suggestions, TIA Shaneo
Chris, thanks for your help...I tried your suggestion but it has now returned the error Error Type: NameError Error Value: global name '_' is not defined If you have time, would you mind explaining what "_" represents? Is this defined in a library that we mightn't have included? Thanks again, Shaneo ----- Original Message ----- From: Chris Kratz To: Shane O'Sullivan ; zope@zope.org Sent: Tuesday, October 30, 2001 4:26 PM Subject: Re: [Zope] calling dtmlMethod from python I ran into this awhile back. Try this: context.scReport(None, _) Interestingly enough, I also learned that you can pass things into a dtml-method and they become available to use internally. For example with the following call: context.scReport(None, _, MsgToUser='Hello') Inside your dtml-method you could do a <dtml-var MsgToUser> This is very usefull to passing stuff around without cluttering up your REQUEST namespace. Another little trick I learned. Want to find an object via acquisition and then call it? I found that the following worked in a python script: methodToCall = _.getitem('methodName') # Find object via acquisition and get handle on it. print methodToCall(None, _) # Call the method and print the results. This is really usefull if you generate the method name as I do depending on the request. ie methodToCall = _.getitem(someStrVar + 'form') will concatenate the two strings and then look for that object in the namespace. If you do this kind of lookups, you should probably also check for existence before calling it, ie _.has_key(someStrVar + 'form'). Hope that helps. -Chris ------------------------------ Chris Kratz chris.kratz@vistashare.com ----- Original Message ----- From: Shane O'Sullivan To: zope@zope.org Sent: Tuesday, October 30, 2001 11:13 AM Subject: [Zope] calling dtmlMethod from python Can anyone please help with this, I'm new to zope and trying to write a simple python script to handle some logic which will then call a dtml method. I'm calling the method as such: context.scReport() The error I'm getting back is Error Type: KeyError Error Value: standard_html_header standard_html_header is being called from the scReport method which suggests the scReport method is being invoked but why are we getting an error?? Any suggestions, TIA Shaneo
Go to your python script and click on the bindings tab. The fourth item has a heading of Namespace. Put the recommended value of _ into the field and save changes. What this does is it binds the namespace that is available to dtml-methods normally to the name _. The problem you were running into is that when you enter a python script, normally this namespace *IS NOT* passed into the script. But, once you bind it to the _ name, you can access it by doing things like _.getitem('') or _.has_key('') etc. The last piece of the puzzle is to pass that namespace into your dtml method so it can access it by passing it as the second parameter. Notice that you can set it to whatever name you want. I think there are historical reasons why it has generally been _. If you do set it to something else, then you need to change _ to your new name in your code. This was one of the biggest frustrations I had and still sometimes have with moving to more python scripted code. Writing python scripts is a lot easier for complex logic. But the namespace stuff seems unnecessarily complex and frustrating sometimes. There is probably an easier way to do this sort of thing and perhaps someone with more experience would like to comment and clarify, but this is how I was able to get my code working. Good Luck, -Chris ------------------------------ Chris Kratz chris.kratz@vistashare.com ----- Original Message ----- From: Shane O'Sullivan To: Chris Kratz ; zope@zope.org Sent: Tuesday, October 30, 2001 12:04 PM Subject: Re: [Zope] calling dtmlMethod from python Chris, thanks for your help...I tried your suggestion but it has now returned the error Error Type: NameError Error Value: global name '_' is not defined If you have time, would you mind explaining what "_" represents? Is this defined in a library that we mightn't have included? Thanks again, Shaneo ----- Original Message ----- From: Chris Kratz To: Shane O'Sullivan ; zope@zope.org Sent: Tuesday, October 30, 2001 4:26 PM Subject: Re: [Zope] calling dtmlMethod from python I ran into this awhile back. Try this: context.scReport(None, _) Interestingly enough, I also learned that you can pass things into a dtml-method and they become available to use internally. For example with the following call: context.scReport(None, _, MsgToUser='Hello') Inside your dtml-method you could do a <dtml-var MsgToUser> This is very usefull to passing stuff around without cluttering up your REQUEST namespace. Another little trick I learned. Want to find an object via acquisition and then call it? I found that the following worked in a python script: methodToCall = _.getitem('methodName') # Find object via acquisition and get handle on it. print methodToCall(None, _) # Call the method and print the results. This is really usefull if you generate the method name as I do depending on the request. ie methodToCall = _.getitem(someStrVar + 'form') will concatenate the two strings and then look for that object in the namespace. If you do this kind of lookups, you should probably also check for existence before calling it, ie _.has_key(someStrVar + 'form'). Hope that helps. -Chris ------------------------------ Chris Kratz chris.kratz@vistashare.com ----- Original Message ----- From: Shane O'Sullivan To: zope@zope.org Sent: Tuesday, October 30, 2001 11:13 AM Subject: [Zope] calling dtmlMethod from python Can anyone please help with this, I'm new to zope and trying to write a simple python script to handle some logic which will then call a dtml method. I'm calling the method as such: context.scReport() The error I'm getting back is Error Type: KeyError Error Value: standard_html_header standard_html_header is being called from the scReport method which suggests the scReport method is being invoked but why are we getting an error?? Any suggestions, TIA Shaneo
On Tue, Oct 30, 2001 at 05:04:21PM -0000, Shane O'Sullivan wrote:
Chris, thanks for your help...I tried your suggestion but it has now returned the error Error Type: NameError Error Value: global name '_' is not defined
If you have time, would you mind explaining what "_" represents? Is this defined in a library that we mightn't have included?
In dtml, the special _ variable is a mapping of the current namespace. So when you do <dtml-let foo=1>, foo now lives inside _ and could be accessed as <dtml-var "_['foo']">. In python scripts, you don't by default have access to the namespace of the calling dtml object. You can get access to it by going to the Bindings tab in the management interface, and set a value for the Namespace binding. You can make it _ or whatever you want, as long as you refer to it by the same name throughout your python script. Chris' example assumes that you have bound Namespace to _. He just forgot to mention that. :) Personally I hate the fact that DTML uses a variable named _. If I made a list of "least favorite things about Zope", that would be at or near the top. Frequently-used variables should not have completely meaningless names. Maybe ZC (nee DC) thought that users wouldn't see that variable very often? It sure comes up a lot on the list. Yet another reason I prefer ZPT over DTML... there's no _ variable and I don't miss it one bit! -- paul winkler home: http://www.slinkp.com music: http://www.reacharms.com calendars: http://www.calendargalaxy.com
participants (3)
-
Chris Kratz -
Paul Winkler -
Shane O'Sullivan