0

So I made a website on Django and wish to deploy it to Google App Engine, the website itself is deployed but without all the static files.

Also, there's a dataset in a .txt file that my website depends upon to run (It's a machine learning project.), but it's not able to find it and the browser just throws a file not found error.

it would be super cool if someone could help me out.

here's the app.yaml file.

runtime: python37
entrypoint: gunicorn -b :$PORT predictweb.wsgi
handlers:
- url: /static
  static_dir: static/
- url: /.*
  script: auto
fortyTwo102
  • 113
  • 1
  • 9
  • https://stackoverflow.com/questions/41414346/gae-file-get-contents-and-static-files – LuisTavares Mar 19 '19 at 22:48
  • 1
    Please don't post text as images. Instead copy-paste it into the post and format it accordingly. – Dan Cornilescu Mar 20 '19 at 01:55
  • 1
    Where is your `.txt` file located (relative to `app.yaml`)? Show the code trying to access it. What's the URL you're using for one of your static files and what's the actual file location? – Dan Cornilescu Mar 20 '19 at 02:04
  • the .txt file is in /project-name/pages/ directory where the views.py file lives. it throws this error OSError at / /srv/pages\songdatabase1.txt not found. Request Method: POST Request URL: Django Version: 2.1.7 Exception Type: OSError Exception Value: /srv/pages\songdatabase1.txt not found. Exception Location: /env/lib/python3.7/site-packages/numpy/lib/_datasource.py in open, line 624 Python Executable: /env/bin/python3.7 Python Version: 3.7.2 – fortyTwo102 Mar 20 '19 at 07:59
  • Good edit. For the future - it's also recommended to just edit the question and add the details in it - comments have poor formatting support, limited size (and may disappear). – Dan Cornilescu Mar 20 '19 at 10:56

2 Answers2

0

At least in the 1st generation standard environment (python 2.7) the static files/directories would by default be separated from the application code, one would have to add application_readable: true to also have a copy together with the app code. See details in the post linked in @lusitanica's comment.

But that's not clearly stated in the 2nd generation handlers doc. The separation is kinda implied:

All of the files in the given directory are uploaded as static files and none of them can be run as scripts.

But there's no mentioning of application_readable. Still, I think it's worth a try, other configs weren't documented for a while but they were working, see comments to this post.

Another thought, assuming that maybe you followed some generic django guide which is typically showing how to server the static content via django - i.e. via the application, not via the CDN-like GAE method (which is how it's served if configured in app.yaml).

It's likely possible to make it work through app.yaml. At least I see in Running Django on App Engine Standard Environment. Maybe check your settings.py, I see in the github version references in the doc:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_ROOT = 'static'
STATIC_URL = '/static/'

But I'm not a django user, can't help with more details.

If none of the above is working you could also just take the static_dir config out of the app.yaml and let django serve the static content. Not optimal, I know - using app CPU cycles.

As for the .txt file,

Exception Value: /srv/pages\songdatabase1.txt not found 

shows a couple of problems:

  • you're mixing windows path separator \ with the *nix one /. You need the *nix one on GAE, but it's better to just use os.path.join() instead of manually manipulating the path
  • you're using a full path - you should use paths relative to the app top dir (where the app.yaml file exists). In your case it should be pages/songdatabase1.txt
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • 1) my settings.py file has those two lines, 2) I tried adding the application_readable bit in the app.yaml file but it just throws an error saying that its not needed in python37 runtime environment. – fortyTwo102 Mar 20 '19 at 08:02
  • I still think it's some django setting. Check carefully the request paths when running locally and on GAE, they're likely assembled wrong. I see the unexpected `/srv` prefix on the `.txt` file, for example. Full paths are OK for static content - URLs – Dan Cornilescu Mar 20 '19 at 11:00
  • I did not use the join() function correctly, its now able to find the txt file. It was an issue of the slashes after all, so thank you so much! – fortyTwo102 Mar 21 '19 at 16:05
0

Here are the handlers that worked for me

handlers:

  - url: /static
    static_dir: static/

  - url: /.*
    secure: always
    redirect_http_response_code: 301
    script: auto
Muhammad Haseeb
  • 634
  • 5
  • 20