python - How to import modules into web.py templates? -
i have following piece of code:
render = web.template.render('templates/') app = web.application(urls, globals())
i read template imports in web.py cookbook.
now, when try import re
in template:
render = web.template.render('templates/', globals={'re':re}) app = web.application(urls, globals())
i got error:
<type 'exceptions.typeerror'> @ /'dict' object not callable
and line showed in traceback: app = web.application(urls, globals())
.
but when modify it:
app = web.application(urls)
the error gone, , re
imported in template.
i don't understand how globals={'re': re}
in web.template.render
breaks it?
why can't keep both globals in second example?
i'm guessing there else doing in script or template causing error. if showed complete example, easier see. here working example:
import web import re urls = ('/', 'index') render = web.template.render('templates/', globals={'re':re}) app = web.application(urls, globals()) class index: def get(self): args = web.input(s='') return render.index(args.s) if __name__ == '__main__': app.run()
and template, index.html:
$def with(s) $code: if re.match('\d+', s): num = 'yes' else: num = 'no' <h1>is arg "$:s" number? $num!</h1>
browse http://localhost:8080/?s=123 try it.
Comments
Post a Comment