8 months ago
I have a Django application deployed on Railway, and I am using the /app/media/
directory to store uploaded files, including images that are part of an event. However, when I try to access the images, I get a 404 error. The logs indicate that the files are present in the /app/media/event_photos/event_12/IMG20241227175657.jpg
directory, but they are not being displayed correctly on the website.
Steps Taken:
I confirmed that the images exist in the correct path
/app/media/event_photos/event_12/IMG20241227175657.jpg
, as verified by thelist-media
command on Railway.The application is running correctly and the files are being saved into
/app/media/
as expected.I have configured the paths in
settings.py
to use/app/media/
for storing files, and I have set upMEDIA_URL
andMEDIA_ROOT
accordingly.
Error:
Despite the correct file paths and their existence, when trying to access the image, I receive a 404 Not Found
error. For example:
Copia
Not Found: /media/event_photos/event_12/IMG20241227175657.jpg
Potential Cause:
It seems that Django is not serving the media files correctly. Even though the images are in the correct location, the application might not be properly configured to serve media files from /app/media/
on Railway.
Possible Configuration Issues:
There may be a misconfiguration in how static and media files are served by Django or Railway.
The URLs for serving media files might not be properly set up in
urls.py
, or there could be issues with the reverse proxy settings on Railway.The media files might not be served correctly due to a configuration issue in Nginx or Gunicorn.
6 Replies
8 months ago
Heya, we're not able to offer application support.
However, the error you sent mentions Not Found: /media/event_photos/event_12/IMG20241227175657.jpg
- is that the http path or the file path ? if it's the file path, it is missing the /app
at the start.
Best,
Nico
Status changed to Awaiting User Response Railway • 8 months ago
8 months ago
Foto eliminata: /app/media/event_photos/event_27/IMG20241227175657.jpg
Not Found: /media/event_photos/event_28/IMG20241227175657.jpg
This is the log file, which is very strange because if I delete the photo, it finds it, but if I try to view it, it doesn't.
Status changed to Awaiting Railway Response Railway • 8 months ago
8 months ago
Well if deletes in your application are working- it's somewhat clear that this is an application issue.
Unfortunately I can't solve this for you so I'm gonna go mark this as solved.
Status changed to Awaiting User Response Railway • 8 months ago
8 months ago
Hello,
Just noticed that you are blocking the static media file serving behind a debug variable within your urls.py file.
If you remove the if statement and place the static functions within the urlpatterns array it should work to serve media in production.
I am aware this is not something the Django developers recommend, but given you are running your application with a production-grade http server (gunicorn) the issue is nearly non-existent for your use case.
Best,
Brody
Status changed to Awaiting User Response Railway • 8 months ago
8 months ago
Thanks for your response. I made the modification you suggested, but it still doesn’t work.
ZIP trovato per l'evento: massimiliano. Inizio estrazione.
Estrazione ZIP in: /app/media/event_photos/event_29
File ZIP event_zips/IMG20241227175657.zip estratto in /app/media/event_photos/event_29
Salvando foto: IMG20241227175657.jpg
Foto IMG20241227175657.jpg salvata nel database.
Not Found: /media/event_photos/event_29/IMG20241227175657.jpg
Not Found: /media/event_photos/event_29/IMG20241227175657.jpg
Not Found: /app/media/event_photos/event_29/IMG20241227175657.jpg
Look at the log file: the procedure executes everything correctly, but in the end, it does not find the file. However, when viewing the folder, the file appears. It seems as if it fails to render the visualization.
Status changed to Awaiting Railway Response Railway • 8 months ago
8 months ago
Hello,
Can you try the syntax shown here -
https://github.com/railwayapp-templates/django-volume/blob/main/mysite/urls.py#L25
Status changed to Awaiting User Response Railway • 8 months ago
2 months ago
This thread has been marked as solved automatically due to a lack of recent activity. Please re-open this thread or create a new one if you require further assistance. Thank you!
Status changed to Solved Railway • 2 months ago