我使用Flask并想更改资产文件夹目录。这是我的文件夹结构:
/python /static /js /img /font /css /templates /default /css /js /img /venv app.py
我想将静态文件夹(js,css,font,img)下的所有文件夹移到该文件夹下default。但是当我这样做时,我的css文件和其他文件(js etc.)无法加载页面。我应该为我的应用程序设置一个属性吗?我也尝试了以下方法:
(js,css,font,img)
default
(js etc.)
app = Flask(__name__,static_path="/templates/default")
但是无法做到这一点,还有其他方法吗?
更新: 当我在(static_path="templates/default")上方的行中删除前导斜杠时,出现了ValueError: urls must start with a leading slash 带有追溯的错误:
(static_path="templates/default")
ValueError: urls must start with a leading slash
Traceback (most recent call last): File "app.py", line 17, in <module> app = Flask(__name__,static_path="templates/default") File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 481, in __init__ view_func=self.send_static_file) File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 63, in wrapper_func return f(self, *args, **kwargs) File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py", line 943, in add_url_rule rule = self.url_rule_class(rule, methods=methods, **options) File "/Users/ozcan/Library/Python/2.7/lib/python/site-packages/werkzeug/routing.py", line 550, in __init__ raise ValueError('urls must start with a leading slash') ValueError: urls must start with a leading slash
初始化应用程序时,它应该是static_folder而不是static_path。
app = Flask(__name__,static_folder="/templates/default")
在模板中:
<script src="{{ url_for('static', filename='js/file.js')}}"</script>
希望能帮助到你。