【问题标题】:Flask: serve template in another folder (other than 'templates/' directory)Flask:在另一个文件夹中提供模板(“模板/”目录除外)
【发布时间】:2017-12-30 00:24:03
【问题描述】:

我已经为我的项目生成了 sphinx 文档到文档文件夹中。现在我想使用烧瓶服务器来提供服务。

这是我的项目结构。我想从文档文件夹中提供example.html。该文件夹还包含所需的相关 CSS 文件。

app/
    module1/
    module2/
    documentation/
        build/
            example.html
            styles.css
            ...
    templates/
        other_files.html
        ...

app_controller.py

​​>
@application.route("/doc", methods=["GET"])
@log_exceptions_to_github
def doc():
    return render_template('/build/index.html')

问题:

Flask 默认只在“模板”文件夹中查找 HTML 文件。

所以我无法提供此服务。请注意,由于项目要求,我不想从文档目录复制到模板目录。

我想将“文档”文件夹与默认文件夹一起添加为模板目录。

我已经尝试过了,但它不起作用: https://stackoverflow.com/a/13598839/2286762

【问题讨论】:

    标签: python flask jinja2


    【解决方案1】:

    查看有关 Flask 中 how to organize templates 的 Flask 文档。

    那么为什么不简单地将您的documentation 目录放在templates 目录下,如下所示:

    templates/
        index.html
        documentation/
            build/
                example.html
        other_directories/
    

    然后只需拨打return render_template('documentation/build/example.html')

    如果你真的想保持你在代码示例中建议的结构,你可以看看关于How to load from more then one template_folder for Flask blueprint?的问题。

    在 Flask 文档中,您可以找到有关 what is a blueprint 的更多信息。

    【讨论】:

    • 已经试过了。文档文件夹也有相关的 css 文件。如果我这样做,那些 Css 路径就会中断。
    • @KishanMehta 任何堆栈跟踪?除了上述答案中描述的方法之外,还可以尝试this 以从static 目录以外的其他位置提供静态文件
    • 好的过程是,我正在使用 sphinx 文档生成命令make html html 是根据 .rst 文件自动生成的。问题是我不能在 sphinx 自动生成的 html 中使用“custom_static”标签。希望我很清楚。
    • @KishanMehta 所以如果我理解正确的话,这是狮身人面像的问题。这听起来像是一个新问题的主题。
    • 好的。我认为可能有一种方法可以添加多个模板和静态文件夹,就像我们在 django 中可以做的那样。无论如何,我会采取另一种方法。
    猜你喜欢
    • 2012-12-09
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2017-08-13
    • 1970-01-01
    相关资源
    最近更新 更多