1

I have developed a website using the latest version of Django, finished all the offline testing, and deployed it to Google App engine. Previously, images and scripts for my website were located in a folder titled ‘scripts’, and running locally I had no problems.

Now that my website is deployed however, no images / scripts / static files are successfully loading, and I cannot figure out an appropriate fix.

I have successfully created a bucket with google cloud, but I can’t seem to create a functioning path to that bucket. I am unsure whether a html address is a valid parameter for the path location in Settings.py, and regardless replacing the simple HTML address of my google cloud bucket for my STATIC_URL parameter has not fixed the problem.

Has anybody else run into this problem, and does anyone know the best configuration for the static_url parameter in Settings.py? Or a simpler way to set up your static files? Thanks in advance!

Alex
  • 195
  • 1
  • 1
  • 7

1 Answers1

2

You need to specify a static directory on your app.yaml file, for example:

handlers:
- url: /scripts
  static_dir: scripts/ 

In this github repo you can find a full example of Django and app engine

keep in mind that the main idea of the static directories is serve content to the front-end, all resources in that path will be publicly accessible

If you need access to another files within your app engine service you need to get the relative directory of your mail script, try to use this code

import os
dirname = os.path.dirname(__file__)
filename = os.path.join(dirname, 'relative/path/to/file/you/want') 
Jan Hernandez
  • 4,414
  • 2
  • 12
  • 18
  • Update, thank you so much for the help! Running collect static and resetting some parameters got my images to load correctly. I still can’t get my scripts to run. This is a bit of a dumb question, but I have a line that calls my scripts, exec(open(“/static/scripts/recalculate.py”).read()) and I keep on getting the error no such file or directory. Any idea what I’m doing wrong? My main problem is that once deployed I’m unsure what the root directory for my website is, so I’m unsure what the complete path is to my script. Thanks again for the help, I am a little out of my league here. – Alex May 01 '20 at 18:35
  • 1
    Is your exec command on python/backend side? I think that is better idea to import that script as a module instead read using exec. but anyway you can get the relative path by using this code ```import os dirname = os.path.dirname(__file__) filename = os.path.join(dirname, 'relative/path/to/file/you/want') ``` https://stackoverflow.com/a/918178/11529321 – Jan Hernandez May 01 '20 at 19:03
  • keep in mind that the main idea of the static directories is serve content to the front-end, all resources in that path will be publicly accessible – Jan Hernandez May 01 '20 at 19:10
  • Update, that did the trick, and I’m going to work on improving my form, cleaning up my scripts. Thank you so much, appreciate the help! – Alex May 01 '20 at 23:16
  • 1
    @Alex if you think that my answer was useful please mark as valid and upvote :) – Jan Hernandez May 04 '20 at 04:54