How to serve static files from Django

Noob here. I’ve successfully deployed a simple hobby Django app connected to Postgres. I created a free 3GB persistent volume but I don’t know what I need to do next to get the files in my static folder to load from there. My Django site opens but I get 404s on the CSS files, images, etc.

I assume I have to edit fly.toml, settings.py, and perhaps something else, but I haven’t found any tutorials guiding me through, and I’m at the level where a tutorial is what I need.

In fly.toml I have:

[mounts]
source="myapp_data"
destination="/app/static"

as well as

[[statics]]
  guest_path = "/app/static"
  url_prefix = "/static/"

In settings.py, I set STATIC_URL = '/static/'. But when I deploy these files seem to be ignored.

Now that I have Django running, what should I do to load static files?

hi! If I’m understanding your question correctly, to get this working I’m pretty sure you’d need to move the static assets off the volume and into your app. The following thread has some additional context about the [[statics]] feature, if you’re curious:

1 Like

Thanks for the link. That thread doesn’t seem to have a resolution, but it does mention adding middleware for static files. I’m wondering then if I should try using something like whitenoise to get the assets off the volume as you describe.

My problem is I don’t have an intuition for what Django and fly are doing after I deploy. I’m new to fly and Docker so there’s a lot of magic going on that makes it hard to diagnose the next step, as obvious as it might be to others.

For anyone else in a similar situation, I got this working by setting up whitenoise.

I added whitenoise to requirements.txt, then updated settings.py by adding 'whitenoise.middleware.WhiteNoiseMiddleware', to MIDDLEWARE and STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'.

Thank you @eli for pointing me in the direction. :slightly_smiling_face:

1 Like

Hi, I tried what you did but i still dont see effects of my static file. Images are being displayed but css files are not being used and it says Not Found: /static/styling/base.css/. Every thing works fine locally. and i check to confirm that the css file is in my app by ssh-ing into the app directory

What are you using for STATICFILES_STORAGE? I was getting 500 errors when I used ManifestStaticFilesStorage in production. Changed to StaticFilesStorage and it worked.

Edit: Realized this is a change I made bypassing whitenoise and doesn’t apply to my original setup.

STATICFILES_STORAGE = “whitenoise.storage.CompressedStaticFilesStorage”
This is my static file storage

Another issue I ran into with the default configuration was making sure STATIC_ROOT was named ‘staticfiles’. If I had the directory named anything else I received a 404.