2011-02-18 1 views
0

는 :모듈을 web.py 템플릿으로 가져 오는 방법은 무엇입니까? 내가 코드 조각 다음 한

render = web.template.render('templates/') 
app = web.application(urls, globals()) 

나는 web.py cookbook에서 템플릿 수입에 대해 읽어보십시오. 이제

, 내가 템플릿에 re을 가져 오려고 :

render = web.template.render('templates/', globals={'re':re}) 
app = web.application(urls, globals()) 

내가 오류가있어 :

<type 'exceptions.TypeError'> at /'dict' object is not callable 

을이 라인은 역 추적에 보여 주었다 : app = web.application(urls, globals())합니다.

그러나 나는 그것을 수정할 때 :

app = web.application(urls) 
오류가 사라

re 내 템플릿에 가져가.

어떻게 globals={'re': re}web.template.render가 깨지십니까?

둘째 예제에서 두 전역 변수를 유지할 수없는 이유는 무엇입니까?

답변

5

나는 그 오류를 일으키는 스크립트 또는 템플릿에서 뭔가하고 있다고 생각합니다. 당신이 완전한 예를 보여 주었다면, 보는 것이 더 쉬울 것입니다.

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() 

그리고 템플릿, index.html을 :

$def with(s) 

$code: 
    if re.match('\d+', s): 
     num = 'yes' 
    else: 
     num = 'no' 

<h1>Is arg "$:s" a number? $num!</h1> 

검색 http://localhost:8080/?s=123에 그것을 시도하는 다음 동작하는 예제입니다.

+0

예를 들어 주셔서 감사합니다. 오늘 서버를 다시 시작했을 때 실수로 문제가 발견되었습니다. 코드가 작동했습니다. 내 잘못이야. – vladimirze