Python Flask静态文件夹在包含参数的页面上不工作
我的静态文件似乎可以在我的所有页面上工作,除非页面使用参数。这是我的密码: 文件夹结构:Python Flask静态文件夹在包含参数的页面上不工作,python,python-2.7,flask,Python,Python 2.7,Flask,我的静态文件似乎可以在我的所有页面上工作,除非页面使用参数。这是我的密码: 文件夹结构: [WEB] myApp.py [templates] test.html [static] [img] my_image.png from flask import Flask, redirect from Cheetah.Template import Template app = Flask(__name__, stat
[WEB]
myApp.py
[templates]
test.html
[static]
[img]
my_image.png
from flask import Flask, redirect
from Cheetah.Template import Template
app = Flask(__name__, static_folder='static')
app.config.from_object(__name__)
app.secret_key = 'THIS_WILL_BE_SOME_RANDOM_KEY'
app.debug = True
@app.route('/')
def main_route():
return redirect('/home')
@app.route('/home')
def home():
return str(Template(file="templates/test.html"))
@app.route('/somePage')
@app.route('/somePage/<var>/')
def somePage(var=None):
return str(Template(file="templates/test.html"))
if __name__ == "__main__":
app.run(host="0.0.0.0")
<html>
<head>
</head>
<body>
<div><img src="static/img/my_image.png"></div>
</body>
</html>
烧瓶应用程序:
[WEB]
myApp.py
[templates]
test.html
[static]
[img]
my_image.png
from flask import Flask, redirect
from Cheetah.Template import Template
app = Flask(__name__, static_folder='static')
app.config.from_object(__name__)
app.secret_key = 'THIS_WILL_BE_SOME_RANDOM_KEY'
app.debug = True
@app.route('/')
def main_route():
return redirect('/home')
@app.route('/home')
def home():
return str(Template(file="templates/test.html"))
@app.route('/somePage')
@app.route('/somePage/<var>/')
def somePage(var=None):
return str(Template(file="templates/test.html"))
if __name__ == "__main__":
app.run(host="0.0.0.0")
<html>
<head>
</head>
<body>
<div><img src="static/img/my_image.png"></div>
</body>
</html>
我做错什么了吗?
我的静态文件夹设置是否不正确?
为什么它可以在/home和/somePage上工作,而不能在/somePage/var上工作?您正在使用相对URL加载静态元素。这不适用于路径中包含多个斜杠的任何URL 使用
url\u for()
函数生成绝对路径:
<div><img src="{{ url_for('static', filename='img/my_image.png') }}"></div>
这个解决方案并没有立即对我起作用,因为我真正的问题是我使用的是Cheetah模板而不是jinja模板。我的网站最初是由cherrypy提供动力的,我经常和cheetah一起使用cherrypy,当我切换到flask进行尝试时,我忽略了切换模板引擎。不管怎样,你的回答帮助我找出了我的错误,你对我使用相对路径的看法基本上是正确的,所以谢谢你。