Flask:如何在模板目录下提供静态文件? [英] Flask : How to serve static files from under the template directory?
问题描述
/ python
我使用Flask并想更改资产文件夹目录。 / static
/ js
/ img
/ font
/ css
/模板
/默认
/ css
/ js
/ img
/ venv
app.py
我想将静态文件夹(js,css,font,img)
下的所有文件夹移动到文件夹 default
下。
但是当我这样做我的CSS文件和其他( js等)
不能加载页面。我应该设置一个属性到我的应用程序?
我也试过这个:
$ $ $ $ $ c $ app $ Flask(__ name __,static_path =/ templates / default)
但是做不到。还有另一种方法可以做到吗?非常感谢。
更新:
当我删除上面一行中的前斜线( static_path =templates / default code>)错误
ValueError:网址必须以前导斜杠开始
跟踪:
Traceback(最近一次调用的最后一个):
在< module>文件中的app.py,第17行。
app = Flask(__ name __,static_path =templates / default)
文件/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py,第481行,在__init__
view_func = self.send_static_file)
文件/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py,第63行,在wrapper_func
返回f(self,* args,** kwargs)
文件/Users/ozcan/Library/Python/2.7/lib/python/site-packages/flask/app.py,第943行,在add_url_rule
rule = self.url_rule_class(rule,methods = methods,** options)
文件/Users/ozcan/Library/Python/2.7/lib/python/site-packages/ werkzeug / routing.py,第550行,在__init__
提高ValueError('网址必须以斜杠开始)
ValueError:网址必须以斜杠开始
$ c
$ div class =h2_lin>解决方案 在初始化应用程序时,它应该是static_folder而不是static_path。
app = Flask(__ name __,static_folder =/ templates / default)
在模板中:
< script src ={{url_for('static',filename ='js / file.js')}}< / script>
希望有帮助。
i use Flask and want to change my assets folder directory.Here's my folder structure:
/python
/static
/js
/img
/font
/css
/templates
/default
/css
/js
/img
/venv
app.py
I want to move all the folders under static folder (js,css,font,img)
under to the folder default
.
But when i do this my css files and the others(js etc.)
can not be loaded the page.Should i set a property to my application?
I also tried this one:
app = Flask(__name__,static_path="/templates/default")
But could not make it.Is there another way to do this?Thanks a lot.
Update:
When i remove the leading slash in the line above (static_path="templates/default"
) got this error ValueError: urls must start with a leading slash
with the traceback:
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
解决方案 It should be static_folder not static_path while initializing app.
app = Flask(__name__,static_folder="/templates/default")
In templates:
<script src="{{ url_for('static', filename='js/file.js')}}"</script>
Hope it helps.
这篇关于Flask:如何在模板目录下提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!