Flask 中关于 url_for 的查询
Query regarding url_for in Flask
我一直在做一个 Flask 项目。但是我 运行 遇到了一个问题。
要正确理解我的问题,您必须查看我的文件夹结构。
我想使用 url_for
语法访问我的 models.py
文件中的“./static/data/wordlist.txt
”。
这是我写的是什么:url_for('static', filename='data/wordlist.txt')
.
但是烧瓶给我一个错误说:FileNotFoundError: [Errno 2] No such file or directory: '/static/data/wordlist.txt'
关于什么的任何帮助我应该做就好了!!
使用 app.url_root
获取相对于您的代码的路径。
wordlist_path = os.path.join(app.root_path, "static/data/wordlist.txt")
with open(wordlist_path) as f:
...
我一直在做一个 Flask 项目。但是我 运行 遇到了一个问题。
要正确理解我的问题,您必须查看我的文件夹结构。
我想使用 url_for
语法访问我的 models.py
文件中的“./static/data/wordlist.txt
”。
这是我写的是什么:url_for('static', filename='data/wordlist.txt')
.
但是烧瓶给我一个错误说:FileNotFoundError: [Errno 2] No such file or directory: '/static/data/wordlist.txt'
关于什么的任何帮助我应该做就好了!!
使用 app.url_root
获取相对于您的代码的路径。
wordlist_path = os.path.join(app.root_path, "static/data/wordlist.txt")
with open(wordlist_path) as f:
...