6

是否有烧瓶功能或简单的方法将静态文件路径转换为磁盘上的绝对文件路径?例如“/static/css/style.css”需要根据应用或蓝图中定义的静态文件夹返回style.css的绝对路径。

澄清一下,我目前正在使用 Flask-Asset。

{% assets "all_js" %}
    <script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}

模板中的上述部分将在生产端生成它。不使用相对路径的主要原因是我可以从无 cookie 域提供静态。

<script type="text/javascript" src="/static/public/all.580e5dae.js"></script>
4

3 回答 3

2

我不清楚为什么需要获取静态文件的路径,但无论如何,如果您查看Flask-Assets的实现,我认为您可以获得一些想法,该实现具有从应用程序中定位静态文件的代码和蓝图,并以不同的方式处理它们。

也许你会发现 Flask-Assets 完全符合你的要求。

于 2013-11-05T02:39:49.993 回答
1

解决的简单方法是使用 'static/css/name_of_css.css'而不是url_for('static', filename='name_of_css.css')

于 2013-11-08T07:58:40.970 回答
0
import os
base_path = os.getcwd() # Replace this appropriately with your static folder path
# We shouldn't use 'static' twice if it is already there in the base_path
# as correctly pointed out by mark-hildreth
if os.path.basename(base_path) == 'static':
    file_path = os.path.normpath('/css/style.css')
else:
    file_path = os.path.normpath('/static/css/style.css')
abs_path = os.path.join(base_path+file_path)
if os.path.exists(abs_path): # Verifies existence of given path
    print abs_path
于 2013-11-04T19:39:12.323 回答