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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -