0

Im trying to use url_for to get a Javascript file named 'game.js'. The file is located inside a folder called 'js' which itself is inside the static folder. The code im using is:

<script src="{{ url_for('static', filename='js/game.js') }}"></script>

This doesn't work. However, if I move the 'game.js' file to be just inside the static folder and not the js folder and use this line of code it works:

<script src="{{ url_for('static', filename='game.js') }}"></script>

Why doesn't it work when the file is inside the js folder?

File tree:

├───static
│   ├───css
│   └───js
|       └───game.js
├───templates
│   ├───layout.html
│   └───index.html
└───app.py
davidism
  • 121,510
  • 29
  • 395
  • 339
BSmith156
  • 91
  • 1
  • 1
  • 4
  • Edit your post with the project tree, the code looks fine to me. – netlemon Jul 14 '20 at 19:45
  • Please update the question with your project's tree – Patch Jul 14 '20 at 19:48
  • @newbie99 added. – BSmith156 Jul 14 '20 at 19:59
  • @Patch added the tree.. – BSmith156 Jul 14 '20 at 20:00
  • Can you maybe view source (Ctrl + U) and show me the path there and does it give a 404 error code when you try to access it? Perhaps it doesn't update the code.. Do that and tell me what happened... – Patch Jul 14 '20 at 20:03
  • @Patch in the source the line is ``. When i access it it is just a blank page, as if it isn't loading the code? – BSmith156 Jul 14 '20 at 20:09
  • @BSmith156 Seems like that... it didn't give you a 404 error... maybe recreated the file? – Patch Jul 14 '20 at 20:10
  • @Patch If I change the line to `` but keep the game.js file in the js folder it works? Strange. Perhaps you don't need to specify folders just the file name? – BSmith156 Jul 14 '20 at 20:13
  • @BSmith156 You definitely do... That is really weird. Are you sure this is your project tree? – Patch Jul 14 '20 at 20:14
  • @Patch Positive, heres a picture to be sure: https://ibb.co/Pr2s2GG. Is there something obvious im missing? – BSmith156 Jul 14 '20 at 20:18
  • @BSmith156 No, looks good that should work.... It's hard to soulve this kind of question without me having the environment – Patch Jul 14 '20 at 20:18
  • @Patch if i revert the code back to the original where it didnt work, run it, and look in the chrome console I see this: https://ibb.co/b3kfdk5. The folder is called 'static/js' instead of being 'js' folder inside 'static'. – BSmith156 Jul 14 '20 at 20:22
  • @BSmith156 That's find chome's doing it because `static` is almost empty, the only folder with something that `static` has is `js` – Patch Jul 14 '20 at 20:27
  • 1
    @Patch Ah ok my bad. Well its working if I remove the js/ from the filename so I'll just leave it as that. I'll let you know if I figure out what's going on, thanks for the help anyway :) – BSmith156 Jul 14 '20 at 20:29

0 Answers0