我试图读取位于路径:templates/print.html
中的文件print.html
。在
所以,我用url_for()
来指代它如下:
{{ url_for('templates', filename='print.html') }}
但是,我得到一个错误,如下所示:
BuildError: ('templates', {'filename': 'print.html'}, None)
这怎么了?在
如果我使用{{ url_for('static', filename='print.html') }}
,它只是在寻找。但是,我试图读取的文件不在static
文件夹中,而是在templates/print.html
中。在
如何使用url_for()
读取templates
文件夹中的文件print.html
?谢谢。在
我先说,如果你试图
url_for
一个模板,很可能你误解了一个概念。但是,假设您知道渲染模板和静态资源之间的区别:您可以为
print.html
建立路由:这将使您能够
url_for('print')
获得解析的url,print.html
模板中的任何动态组件都将呈现为静态html。在否则,如果
print.html
是真正静态的,只需将其移动到static
文件夹中,并按照您的指示使用url_for('static', filename='print.html')
。在有一个方便的Flask blueprint模式非常有用,如果您只是渲染一堆模板,而不需要任何视图逻辑,see it here
相关问题 更多 >
编程相关推荐