I am trying to pass a value from a form to a function, process it, and then call the function in another function. Basically, the user submits a search term. It is passed to a function called process which returns the now processed term as a dictionary. In the results function, the term is parsed to a search engine API and the results are returned in a HTML or JSON format. The function process is returning none instead of the processed term however. Can anyone tell me what I’m doing wrong? This is being done in web2py so some of the code may look odd, but I think the problem is a python one, rather than a web2py
import urllib2
def index():
form = FORM('',
INPUT(_name='query', requires=IS_NOT_EMPTY()),
INPUT(_type='submit'))
if form.process().accepted:
redirect(URL('results'))
elif form.errors:
response.flash = 'form has errors'
else:
response.flash = 'please fill the form'
return dict(form=form)
def __process():
term=request.vars.query
#do some processing
return dict(term=term)
def results():
import urllib2
address = "http://www.blekko.com/?q=%(term)s+/json&auth=<mykey>" % __process()
results = urllib2.urlopen(address).read()
return dict(results=results)
It’s web2py issue.
When you redirect() in web2py, request.vars are not passed on to the new page. session vars do though.
Try printing request.vars inside the form.process().accepted and then again in the __process().
Instead, do the processing all inside the index() and then return the result dict to index view. Then you can redirect or open a new window pass the data along.
Or if you’d rather keep the things way they are, store it in the session for it to be accessible from __process().