mardi 23 juin 2015

how to return a dictionary with template variables

I'm working with the python web framework Bottle to generate html templates, and I want to pass a dictionary of template variables from the bottle server to the page. The documentation says that can be done like this:

@route(’/hello/<name>’)
@view(’hello_template’)
def hello(name=’World’):
  return dict(name=name)

And the @view template should have access to name. I want to do the same thing with a dictionary of search results, simplified here:

@get('/search')
@view('search')
def search():
    // load results
    // return results
    results = {'1': {'param1': 'val1', 'param2': 'val2', 'param3': 'val3'},
               '2': {'param1': 'val1', 'param2': 'val2', 'param3': 'val3'}
              }
    return results

But I get this error:

TypeError('template() keywords must be strings',)

With this traceback:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/bottle.py", line 862, in _handle
    return route.call(**args)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/bottle.py", line 1732, in wrapper
    rv = callback(*a, **ka)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/bottle.py", line 3619, in wrapper
    return template(tpl_name, **tplvars)
TypeError: template() keywords must be strings

I could find similar questions but they all assumed that the params could be correctly passed, so I decided to ask. Thanks!

Aucun commentaire:

Enregistrer un commentaire