烧瓶应用模板找不到文件,返回 404 错误



这太令人尴尬了。我正在flask中的一个flash游戏网站上工作,在真正将flash游戏加载到该网站之前,它一直运行得很好。由于某些原因,游戏找不到.swf文件,返回404错误。

这是模板文件的正文内容。它的位置是myproject/templates/game.html:

<h1>Game success! {{gamedictionary["name"]}}</h1>
<object width="{{gamedictionary['width']}}" height="{{gamedictionary['height']}}">
<param name="movie" value="{{gamedictionary['game_file']}}">
<embed src="{{gamedictionary['game_file']}}" width="{{gamedictionary['width']}}" height="{{gamedictionary['width']}}">
</object>

gamedictionary是请求传入的字典,其中包含游戏的所有数据,如flash游戏的名称、id、宽度和高度,最重要的是它的文件位置。对于红球,它的字典看起来是这样的:

{
"id": "redball1",
"name": "Red Ball",
"game_file": "../resources/game_files/redball1.swf",
"icon_file": "../resources/icon_files/redball1.png",
"desc": "Red Ball is a fun game!",
"width": 786,
"height": 571.63
}

我不认为这是错误所在,但如果有帮助的话,这是app.py的请求函数,它返回game.html模板:

@app.route('/game/<string:gameid>')
def game(gameid):
for gamedictionary in mother_data:
if gamedictionary["id"] == gameid:
print("Found match!")
return render_template("game.html", gamedictionary=gamedictionary)
else:
return render_template("404.html")

(app.py在myproject/app.py中(

for循环遍历mother_data,它是所有游戏数据字典的数组,如果字典的id与请求url匹配(这是匹配的游戏(,则返回game.html模板,并传递包含所有数据的gamedictionary,如上面的代码片段所示。

这就是问题产生的地方。看看最终渲染的html,所有其他游戏数据加载都很好。游戏的名称,闪光框的宽度和高度,一切都很好。问题是游戏找不到该文件。它在控制台中返回此错误。

:5000/resources/game_files/redball1.swf:1 Failed to load resource: the server responded with a status of 404 (NOT FOUND)

我该怎么修复才能找到redball1.swf文件并加载游戏?请温柔一点,我已经很久没有在烧瓶中编程了,即使在那时我还是一个初学者。如果您需要更多信息,我很乐意提供,并回答有关代码其他部分的任何问题。谢谢你的帮助!

找到了解决方案。Flask无法识别任何外部文件夹,只能识别static/和templates/等文件夹。已将我的资源/文件夹更改为static/。工作起来很有魅力!

最新更新