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: