2014-10-29 8 views
0

결국 마침내 Mako이 작동하는 것으로 보입니다. 적어도 콘솔에서 수행되는 모든 작업이 작동합니다. 이제 index.htmlMako으로 렌더링하려고 시도했지만 모두 공백 페이지입니다.python/cherrypy의 Mako 템플릿 렌더링을 통한 빈 페이지

def index(self): 
    mytemplate = Template(
        filename='index.html' 
       ) 
    return mytemplate.render() 

있는 HTML이 이것이다 : 이 내가 부르는 모듈

<!DOCTYPE html> 
<html> 
<head> 
<title>Title</title> 
<meta charset="UTF-8" /> 
</head> 
<body> 
<p>This is a test!</p> 
<p>Hello, my age is ${30 - 2}.</p> 
</body> 
</html> 

를 그래서 호출 할 때 192.168.0.1:8081/index (이것은 내가 실행하는 로컬 서버 설정이다)가 기능을 시작하지만 결과 내 브라우저에서 빈 페이지입니다.

Mako을 올바르게 이해 했나요?

답변

0

기본 사용법에서는 모든 것이 매우 간단하며 well documented입니다. 엔진에 올바른 경로를 제공하기 만하면됩니다. 파이썬 파일을 함께

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 


import os 

import cherrypy 
from mako.lookup import TemplateLookup 
from mako.template import Template 


path = os.path.abspath(os.path.dirname(__file__)) 
config = { 
    'global' : { 
    'server.socket_host' : '127.0.0.1', 
    'server.socket_port' : 8080, 
    'server.thread_pool' : 8 
    } 
} 


lookup = TemplateLookup(directories=[os.path.join(path, 'view')]) 


class App: 

    @cherrypy.expose 
    def index(self): 
    template = lookup.get_template('index.html') 
    return template.render(foo = 'bar') 

    @cherrypy.expose 
    def directly(self): 
    template = Template(filename = os.path.join(path, 'view', 'index.html')) 
    return template.render(foo = 'bar') 



if __name__ == '__main__': 
    cherrypy.quickstart(App(), '/', config) 

, view 디렉토리를 생성하고 index.html 아래에 다음을 넣어.

<!DOCTYPE html> 
<html> 
<head> 
    <title>Title</title> 
    <meta charset="UTF-8" /> 
</head> 
<body> 
    <p>This is a ${foo} test!</p> 
    <p>Hello, my age is ${30 - 2}.</p> 
</body> 
</html> 
+0

특히 server.py와의 차이점이 어디 있는지는 잘 모르겠지만 (나중에 살펴볼 것입니다.) 저는 기초로 사용하고 실제적으로 작업을 시작할 수 있습니다. 친절하게 감사드립니다. – iBaer

+0

예제에서 인덱스와 직접 모듈의 차이점은 무엇입니까? 둘 다 똑같은 것처럼 보이지만 단순히 서버를 시작할 때 "색인"모듈 만 사용한다고 가정합니다. – iBaer

+0

''TemplateLookup''은 파일 시스템 검색입니다. 템플릿 루트 디렉토리가 어디에 있는지 한 번 알려주고, 나중에''lookup.get_template ('user/cabinet.html')''과 같은 템플릿을 가져 오도록 요청하십시오. 직접 템플릿 파일 이름을 지정하는''직접 ''메소드에서와 같이, 같은 일을 혼자서 할 수 있습니다. 관련 문서 페이지에 대한 링크를 제공했습니다. 선택한 템플릿 엔진이 어떻게 작동하는지 이해하려고 노력하십시오. – saaj