使用 bottle framwork 在 tpl 中显示文本文件内容的问题

Problems with displaying the content of a text file in a tpl using the bottle framwork

到目前为止,我一直在尝试在模板中显示文本文件的内容,但没有成功。这是 到目前为止我的代码:

@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>

我知道我可以 return static_file() 但我需要设计页面 一些 css 和以后的东西。

提前致谢,如果我的英语不正确,请见谅!

您误解了 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)

这应该可以解决问题。

[顺便说一句,第一个问题很好!]