Hello,
Regardless of whether or not that "if" statement is inside a web.py
template or not, it will always raise a KeyError if there is no 'next'
key in the 'results' dictionary, so your problem is more of a dictionary
membership issue rather than a web.py syntax one. So, you need to check
that the 'next' key exists in the 'results' dictionary before you try to
use it, with something like:
$if 'next' in results:
<a href=$results['next']>Click here for more results</a>
----
Regards,
Marios Zindilis
On 07/14/2015 12:58 AM, [email protected] wrote:
Hello all, I'm new to web.py and working on using it with some vendor APIs.
Basic overview, I have the user input some text, web.py takes that text
and sends it to a python script that queries the API, the API returns
results and the results are rendered into a form. The results are always
python dictionary items.
The problem I am having is using IF statements to test if a dictionary
key exists. For instance, the API will return a dictionary key 'next'
with a URL as its value if there are more results to search display. If
there are no more results the 'next' key is not present.
In my template the code looks like
$if results['next']
<a href=$results['next']>Click here for more results</a>
This works fine if the API returns that there are more results to
display. If there are no more results I get an Key Exception error that
the key 'next' does not exist, when I want it to just continue rendering
the page without creating the href tag and link.
I've even added an 'else' statement to handle any Key errors but that
did not work. I've tried using 'Try' 'Except' catches as well but the
page will always fail to load with the Key Exception error.
Am I doing something wrong, or are the IF statements in web.py not
working as they should in normal python?
--
You received this message because you are subscribed to the Google
Groups "web.py" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to [email protected]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/webpy.
For more options, visit https://groups.google.com/d/optout.