我的问题与本网站上的其他几个相似,但我在那里找到的答案并不适合我。烧瓶未在我的软件包的“静态”目录中找到文件
我正在学习烧瓶。我在使用Vagrant VM的Ubuntu 10.04机器上运行Flask 0.10.1和Python 2.7。
我已经尝试过无数次SO,Flask文档和Miguel Grinberg的建议,但都没有成功。
因此,我展示了最简单的版本,我认为可能工作(但不是)为您的细读。
注释掉的替代行(在page_a.html和page_b.html中)做工作但是很可怕。
首先,这里是我的项目的根“树”的输出:
这里是文件(减去在html的有点样板)
page_a.html :
<head>
<link rel="stylesheet" href="styles.css">
<!-- <link rel="stylesheet" href="static/styles.css"> -->
</head>
<body>
<h1>page_a</h1>
<img src="an_image.png">
<!-- <img src="static/an_image.png"> -->
<a href="{{ url_for('page_b') }}">to page b</a>
</body>
page_b.html:
<head>
<link rel="stylesheet" href="styles.css">
<!-- <link rel="stylesheet" href="../../static/styles.css"> -->
</head>
<body>
<h1>page_b</h1>
<img src="../../static/an_image.png">
<!-- <img src="../../static/an_image.png"> -->
<a href="{{ url_for('page_a') }}">to page a</a>
</body>
INIT的.py:
from flask import Flask
app = Flask('my_pages')
import my_pages.views
runserver.py:
from my_pages import app
app.run(host='0.0.0.0', port=5000, debug=True)
views.py:
from my_pages import app
from flask import render_template
@app.route('/')
@app.route('/page_a')
def page_a():
return render_template('page_a.html')
@app.route('/pages/page_b/')
def page_b():
return render_template('page_b.html')
styles.css的:
body {
background-color: green;
}
此版本确实工作时,在page_a.html和page_b.html中,我使用注释行(而不是上面的行)。
这里的runserver.py的输出,当我访问page_a.html:
"GET /page_a HTTP/1.1" 200 -
"GET /styles.css HTTP/1.1" 404 -
"GET /an_image.png HTTP/1.1" 404 -
和page_b.html。
"GET /pages/page_b/ HTTP/1.1" 200 -
"GET /pages/page_b/styles.css HTTP/1.1" 404 -
(最后显示 'an_image.png' 从我的 '风格' 目录)
我的问题:我在想什么?这个设置可以在没有重大重构的情况下工作吗?
我当然不想硬编码每个静态文件的完整路径。另外,在实际应用中,这些URL运行深度较深 - 例如,,
http://localhost:5000/book/<id_1>/chapter/<id_2>/page
非常感谢任何人可能会回复!
它的工作原理!非常感谢你的回应,以及你为什么*它能够工作的清晰,简洁和可理解的解释! – jazcap53