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!
I realy don't understand how you get this error, but anyway your code equals to:
**
means thattemplate()
function must unpackresults
to keywords arguments. In other words this code :Absolutley equals this :
And even equals this :
Now ask yourself how do you access variable named
1
or2
( you trying to use this variables ) in your template ?In your case
template()
function unpacksresults
to keyword arguments. If you want to passresults
into template then usereturn dict(results=results)
But if you provide real code i think you'll get more detailed answer