Hi List, Can't figure this out - need help! In a Script (Python), I'm testing for the presence (or more precisely the absence) of a key in the request, like so: - request=context.REQUEST if not request.has_key('the_key'): The problem is that the test always fails (e.g. it acts as though the key is absent, even when it is not). If I modify the test to check for a form filed, it works fine. What am I doing wrong? -- Regards, PhilK Email: phil@xfr.co.uk / Voicemail & Facsimile: 07092 070518 "The lyf so short, the craft so long to learne" - Chaucer
Philip Kilner wrote:
Hi List,
Can't figure this out - need help!
In a Script (Python), I'm testing for the presence (or more precisely the absence) of a key in the request, like so: -
request=context.REQUEST
if not request.has_key('the_key'):
The problem is that the test always fails (e.g. it acts as though the key is absent, even when it is not). If I modify the test to check for a form filed, it works fine.
What am I doing wrong?
Request is not a dictionary, it just behaves simmilar. You could use sommething like: the_key = request.get('the_key', None): if the_key ... robert
Philip Kilner wrote:
if not request.has_key('the_key'):
Try: if not request.form.has_key('the_key'): ...if you're worried about for mvariables or URL parameters. Otherwise: value = request.get('the_key'): if value is not None: cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
Hi Chris, Robert, Thanks for your replies! Chris Withers wrote:
if not request.form.has_key('the_key'):
...if you're worried about for mvariables or URL parameters.
Otherwise:
value = request.get('the_key'): if value is not None:
The variable actually isn't in the form, but is maintained by the script - although I've put it in the form just to make it work! robert rottermann wrote:
Request is not a dictionary, it just behaves simmilar. You could use sommething like: the_key = request.get('the_key', None): if the_key ...
Didn't realise request was not a dictionary - explains a lot, but am mystified by how this /used/ to work! (This is not actually new code, but a refactoring exercise - but I've lost my "datum" somewhere along the way, and can't face all the "undo"-ing! Will test and come back for posterity! -- Regards, PhilK Email: phil@xfr.co.uk / Voicemail & Facsimile: 07092 070518 "The lyf so short, the craft so long to learne" - Chaucer
Philip Kilner wrote at 2004-3-3 08:43 +0000:
Can't figure this out - need help!
In a Script (Python), I'm testing for the presence (or more precisely the absence) of a key in the request, like so: -
request=context.REQUEST
if not request.has_key('the_key'):
This looks good. "request.has_key('the_key')" will return True when "request" has key "'the_key'" (the literal string 'the_key') -- Dieter
participants (4)
-
Chris Withers -
Dieter Maurer -
Philip Kilner -
robert rottermann