1
我在與瓶蟒蛇的問題,我有以下代碼瓶蟒蛇渲染可變文本和不是HTML
import glob
import os
from bottle import run, route, error, template
from Find_Posts import hyperlink_postnames
currentdir = os.getcwd()
def hyperlink_postnames():
hyperlink_filelist = []
os.chdir(currentdir + "\\Blog_Posts\\")
for files in glob.glob("*.txt"):
hyperlink_filelist.append('<a href = "/blog/' + files + '"' + '>' + str(os.path.splitext(files)[0]) + '</a>')
return hyperlink_filelist
返回下面的列表
['<a href = "/blog/post1.txt">post1</a>', '<a href = "/blog/post2.txt">post2</a>', '<a href = "/blog/post3.txt">post3</a>', '<a href = "/blog/post4.txt">post4</a>', '<a href = "/blog/post5.txt">post5</a>', '<a href = "/blog/post6.txt">post6</a>']
這又餵給以下的瓶裝路線:
@route('/blog/')
def postnames():
postlist = hyperlink_postnames()
tpl_out = template('blogroll', postlist = postlist)
return tpl_out
它被送入blogroll.tpl模板:
<!DOCTYPE html>
<div>
<p><b>Blog Roll</b></p>
%for postlist in postlist:
<li> {{ postlist }}
%end
</div>
我的問題是,當我呈現在瀏覽器中它把postlist變量模板爲純文本,而不是HTML(這是什麼列表裏面寫的)的模板,但是,如果我改變瓶子代碼這樣寫的(繞過模板)它呈現postlist變量作爲HTML,但不是這使得代碼沒用的模板中:
@route('/blog/')
def postnames():
postlist = hyperlink_postnames()
tpl_out = template('blogroll', postlist = postlist)
return postlist #return the variable directly bypassing the template renders the list as html
沒有任何人有任何想法,爲什麼會這樣呢?
感謝您的解決,這個問題已經準備就緒,看着文檔肯定忽略了它,同時沮喪地敲擊鍵盤! – 2012-08-02 14:34:05