【问题标题】:Flask app template can't find file, returns 404 errorFlask 应用模板找不到文件,返回 404 错误
【发布时间】:2020-11-03 05:06:59
【问题描述】:

这很尴尬。我正在烧瓶中开发一个 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游戏的name、id、width和height,最重要的是它的文件位置。对于 redball,它的字典如下所示:

{
  "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
}

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

@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,里面有所有的数据,见上面的代码sn-p。

这就是问题所在。查看最终渲染的 html,所有其他游戏数据都可以正常加载。游戏的名称,flash frame 的宽度和高度,都可以。问题是游戏找不到文件。它在控制台中返回此错误。

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

我该如何解决它才能找到 redball1.swf 文件并加载游戏?请各位温柔,我很久没用flask编程了,那时候我还只是个初学者。如果您需要更多信息,我很乐意提供,并回答有关代码其他部分的任何问题。感谢您的帮助!

【问题讨论】:

    标签: python api flask filesystems jinja2


    【解决方案1】:

    找到了解决办法。 Flask 无法识别任何外部文件夹,只能识别诸如 static/ 和 templates/ 之类的文件夹。将我的资源/文件夹更改为静态/。像魅力一样工作!

    【讨论】:

      猜你喜欢
      • 2020-12-23
      • 1970-01-01
      • 2021-10-30
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 2021-04-06
      • 2020-06-13
      • 2018-10-27
      相关资源
      最近更新 更多