【问题标题】:Problems with displaying the content of a text file in a tpl using the bottle framwork使用瓶子框架在 tpl 中显示文本文件内容的问题
【发布时间】:2015-01-08 22:44:05
【问题描述】:

到目前为止,我试图在模板中显示文本文件的内容,但没有任何运气。这是 到目前为止我的代码:

@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 和其他内容。

提前致谢,如果我的英语不正确,我们深表歉意!

【问题讨论】:

    标签: python return text-files bottle static-files


    【解决方案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)
    

    这应该可以解决问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-25
      • 2016-06-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2020-10-23
      相关资源
      最近更新 更多