我使用Flask并想要更改我的资产文件夹目录.这是我的文件夹结构:
/python
/static
/js
/img
/font
/css
/templates
/default
/css
/js
/img
/venv
app.py
我想将静态文件夹(js,css,font,img)下的所有文件夹移动到默认文件夹下.
但是当我这样做时,我的css文件和其他(js等)无法加载页面.我应该将属性设置为我的应用程序吗?
我也试过这个:
app = Flask(__name__,static_path="/templates/default")
但是无法做到.有另外一种方法吗?非常感谢.
更新:
当我删除上面一行中的前导斜杠(static_path =“templates / default”)时出现此错误ValueError:urls必须以前导斜杠开头
跟踪:
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
解决方法:
初始化app时应该是static_folder而不是static_path.
app = Flask(__name__,static_folder="/templates/default")
在模板中:
<script src="{{ url_for('static', filename='js/file.js')}}"</script>
希望能帮助到你.