#Media files not loading nor saving

18 messages · Page 1 of 1 (latest)

summer bluffBOT
#

Project ID: N/A

civic forumBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

fading yoke
#

I could have swarn you had a thread open for this already? but apologies if that was a different person

#

you did, and you deleted it even though you had a response

#

do you have a volume mounted, and if so, what is the mount point?

#

yeah that seems good to me, I have a few ideas but I'm not a python dev so no promises

#

mount volume to /app/media [done]

set MEDIA_URL to media/

add a url pattern to urls.py like so to setup a media server

urlpatterns += [ re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})]
#

@cold edge - am I missing anything here?
I think their MEDIA_ROOT is fine as I'd like to not recommend the volume mont path environment variable because that makes local development more cumbersome

cold edge
#

Settings.py


# Media Files (uploaded from users)
MEDIA_URL = "media/"
MEDIA_ROOT = os.environ["RAILWAY_VOLUME_MOUNT_PATH"]

urls.py

urlpatterns += [ re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})]

You will also want to configure white noise for this

Settings.py - middle ware

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]```
#

That's how I have my template configured and it will serve media that is uploaded using white noise

#

Normally in production, you would have an nginx proxy serving your media

#

But, that's a pain to setup within a single docker container like railway deploys apps

fading yoke
cold edge
# fading yoke missed the second half of my message eh 🤣

Nope, I saw it. That's the config that will be needed when the code is deployed. They can wrap the media root in an if statement and check the environment variables of DEBUG, to determine if it reads a default path, or if it reads the railway volume mount path

fading yoke
#

their media root would result in /app/media the same path as the volume is mounted to though

cold edge
#

Yes, but there are read/write perm issues with volumes that it's better to use the environment variables when referencing the location

#

But then again, that is what happened about 6 months ago when I built the template that those configs are from.

fading yoke
#

re-upload it?