2015-01-08 8 views
2

지금까지 템플릿을 사용하지 않고 텍스트 파일의 내용을 표시하려고합니다. 이것은 지금까지 내 코드 입니다 :병 프레임 워크를 사용하여 tpl에 텍스트 파일의 내용을 표시 할 때의 문제점

@route('/show_article/<filename>') 
def show_article(filename): 
    stat_art=static_file(filename, root="articles") 

    return template('show_article', stat_art=stat_art) 

그리고 이것은

<p> 
    {{stat_art}} 
</p> 

내가 난 그냥 static_file를 (반환 할 수 있음을 알고있는 파일의 내용을 표시하는 내 템플릿의 단락입니다)하지만 일부 CSS와 함께 나중에 페이지를 디자인해야합니다.

미리 감사드립니다. 내 영어가 정확하지 않으면 죄송합니다.

답변

1

static_file의 오해가 있습니다.

다행히, 수정은 간단하다 : 단지 파일을 직접 읽고과 같이, 템플릿에 내용을 전달합니다 트릭을 할해야

@route('/show_article/<filename>') 
def show_article(filename): 
    with open(filename) as f: # <-- you'll need the correct path here, possibly including "articles" 
     stat_art = f.read() 

    return template('show_article', stat_art=stat_art) 

합니다.

[Btw, 좋은 첫 번째 질문!]