Flask app template can't find file, returns 404 error

This is embarrasing. I’m working on a flash game site in flask and it’s been going pretty well up until actually loading the flash game in on the site. For some reason, the game can’t find the .swf file, it returns a 404 error.

This is the body contents of the template file. It’s location is 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 is the dictionary passed in by the request, which contains all the data of the game, such as name, id, width and height of the flash game, and most importantly it’s file location. For redball, it’s dictionary looks like this:

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

I don’t think this is where the bug is, but if it helps, this is the app.py’s request function that returns the game.html template:

@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 is in myproject/app.py)

The for loop loops through mother_data, which is an array of all the game data dictionaries, and if the id of the dictionary matches the request url, (it’s the matching game) then it returns the game.html template, and passes gamedictionary, which has all the data, as seen in the above code snippet.

Here’s where the problem arises. Looking at the final rendered html, all of the other game data loads in fine. The name of the game, the width and height of the flash frame, it all works. The issue is that the game can’t find the file. It returns this error in the console.

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

What do I fix so it will find the redball1.swf file and load the game in? Please be gentle, I haven’t programmed in flask for a long time, and even back then I was still only a beginner. If you need any more info I’m happy to give it, and answer any questions about other parts of the code. Thanks for helping!

Add Comment
1 Answer(s)

Found the solution. Flask can’t recognize any outside folders, only ones such as static/ and templates/. Changed my resources/ folder to static/. Works like a charm!

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.