R
Railway14mo ago
Iceman

Where can you store Media Files?

I'm using Django and have added a blog module to it where users can upload their own images. I was wondering whether it is possible to store those images on Railway? Thanks!
12 Replies
Percy
Percy14mo ago
Project ID: N/A
Iceman
Iceman14mo ago
N/A
jonbeau
jonbeau14mo ago
Hello, you'll want to use a volume to store persistent data. https://docs.railway.app/reference/volumes
Railway Docs
Volumes | Railway Docs
Documentation for Railway
Iceman
Iceman14mo ago
Hi, thanks for letting me know. I added a volume to my web app but wasn't able to get it to work. It's linked to the project but nothing seems to get added to it despite the URLs pointing to /media/ as well
Brody
Brody14mo ago
please reference this template https://railway.app/template/AWUIv6
Iceman
Iceman14mo ago
I took a look at the template and implemented its suggestions which seemed to be changing the media root to MEDIA_ROOT = os.environ["RAILWAY_VOLUME_MOUNT_PATH"] and then the volume’s Mount Path to /app/media. After doing that, I can now see that the files are uploading somewhere as the disk usage is increasing. The problem is the files are still not showing up on Wagtail Admin (the django blog app) and the image links are not working. Any suggestions on what to try next? I’m pretty much stumped.
jonbeau
jonbeau14mo ago
Are the links pointing to the correct location? Maybe you also have to configure wagtail to work with the new path.
Iceman
Iceman14mo ago
Do you know if there's any way to access the volume manually through the command line?
jonbeau
jonbeau14mo ago
There is not that I know of, but you could probably use this. https://railway.app/template/Nan7Bs There is another one for downloading.
Brody
Brody14mo ago
By default, the storage location is set to the storage subdirectory in the root of the volume, but by setting a service variable USE_VOLUME_ROOT to 1 you can opt to use the root of the volume as the storage location instead
By default, the storage location is set to the storage subdirectory in the root of the volume, but by setting a service variable USE_VOLUME_ROOT to 1 you can opt to use the root of the volume as the storage location instead
Iceman
Iceman14mo ago
I finally resolved this by reviewing all the code in the django-volumes example. The guy who wrote this wasn't really clear on what changes to make so it took ages to figure out. Also, he seems to be using an unrecommended workaround which isn't meant for production. It was this line that ultimately got it working: urlpatterns += [ re_path(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT})] This is where in the documentation it clearly states that this is for local development only: https://docs.djangoproject.com/en/4.2/ref/views/#django.views.static.serve Is it better to just split user-uploaded files off and serve them from an S3 bucket?
Django Project
Django
The web framework for perfectionists with deadlines.
Brody
Brody14mo ago
yes that isn't recommended for production but as that template is a starter project it is perfectly adequate for its intended usecase, and as your project grows and you learn more about web development you can implement a static file server with caddy or nginx yourself
Want results from more Discord servers?
Add your server