R
Railway13mo ago
x4zx

How do I get the volume files right in django?

There is a django project. The screenshots show the following settings. I created a 1 gigabyte volume and specified it as /media - which is what I actually need. But I must have configured it wrong and when I try to navigate to the image path there is an error (screenshot 3). Can you please tell me how to connect volume to django correctly?
75 Replies
Percy
Percy13mo ago
Project ID: 6a37fcf0-b45a-43ea-aec7-9aac046bdf7f
x4zx
x4zx13mo ago
6a37fcf0-b45a-43ea-aec7-9aac046bdf7f
x4zx
x4zx13mo ago
x4zx
x4zx13mo ago
The path is correct, but the site can't get the photo
Brody
Brody13mo ago
does MEDIA_ROOT tell django where to place the uploaded files?
x4zx
x4zx13mo ago
Yes, here is a screenshot from setting.py. You can also look at the structure of all the files.
Brody
Brody13mo ago
well if you mounted the volume to /media then /media would be your media root but you have prefixed it with the base dir
x4zx
x4zx13mo ago
MEDIA_URL = 'media/' - this, as far as I know, goes in the url address to some photo or video, so it can essentially be called whatever you want. But MEDIA_ROOT = BASE_DIR / 'media' is the name of the folder for images itself.
Brody
Brody13mo ago
yeah so whatever BASE_DIR / 'media' evaluates to, would not be the volume mount path if you want the media root to be the volume, you need /media set as the media root assuming the mount point is /media
x4zx
x4zx13mo ago
So, I did as you said. Now let's see if it works.
Brody
Brody13mo ago
thats not at all what i said lol remove BASE_DIR!! BASE_DIR is not the volume mount!!
x4zx
x4zx13mo ago
so?
Brody
Brody13mo ago
yes, you want files to be saved in the volume thats mounted at /media then that is exactly what the path needs to be, i have not touched django but that now looks correct to me
x4zx
x4zx13mo ago
For some reason it still doesn't find
x4zx
x4zx13mo ago
Is there any way to see what's in volume right now?
Brody
Brody13mo ago
theres not volumes is still beta after all have you uploaded that file first?
x4zx
x4zx13mo ago
https://lyceum208.up.railway.app | I created a user for you. username: brody192 ; password: 1111111111 Initially, after a successful launch, I go into the admin panel and want to change the avatar of one of the teachers. But after I updated the avatar, I want to go and see if it is saved and I get this error
Brody
Brody13mo ago
do you have the correct config in your urls.py?
x4zx
x4zx13mo ago
app: teacher -> urls.py
Brody
Brody13mo ago
if settings debug what do you have debug set to
x4zx
x4zx13mo ago
False
Brody
Brody13mo ago
then those extra urlpatterns arent getting added?
x4zx
x4zx13mo ago
No, they don't.
Brody
Brody13mo ago
that could be a problem?
x4zx
x4zx13mo ago
Honestly, I have no idea.
Brody
Brody13mo ago
remove if statement?
x4zx
x4zx13mo ago
So it doesn't work when in production mode
Brody
Brody13mo ago
why would you want it to not work?
x4zx
x4zx13mo ago
This code is only needed to make static and media files in development mode correctly:
If settings.DEBUG:
from django.conf.urls.static import static

urlpatterns += static(
settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
If settings.DEBUG:
from django.conf.urls.static import static

urlpatterns += static(
settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
That is, when DEBUG=True
Brody
Brody13mo ago
but is it? what if it is needed in production
x4zx
x4zx13mo ago
There is no need for this in production You can basically delete this bit of code
Brody
Brody13mo ago
how can you be sure though? it isn't working right now
x4zx
x4zx13mo ago
Because here there is a check If settings.DEBUG: - works only in development mode. And in production mode it is the host that loads the files
Brody
Brody13mo ago
would it hurt to try and remove that check
x4zx
x4zx13mo ago
No, it won't. Without it, everything will work just as well. But with media we need to figure out why the site can't get photos from there
Brody
Brody13mo ago
well are there any error logs in the logs?
x4zx
x4zx13mo ago
no
x4zx
x4zx13mo ago
Brody
Brody13mo ago
just seems like django isn't setup properly to read the files back out of the media volume
x4zx
x4zx13mo ago
Maybe, but here is my whole settings.py
Brody
Brody13mo ago
media root doesn't have a forward slash prefixing it probably not the core issue but still
x4zx
x4zx13mo ago
Should I delete the media folder I created?
Brody
Brody13mo ago
that wouldn't interfere with anything, since the volume mounts to /media relative to the root of the container
x4zx
x4zx13mo ago
Is it possible to view the contents of /media with the code. Through the same os.listdir()?
Brody
Brody13mo ago
I'm sure there is, but that would be a question for google
x4zx
x4zx13mo ago
I plugged "Volume" into the following code and this is what I got
x4zx
x4zx13mo ago
After connecting "Volume" to the project, the variable "RAILWAY_VOLUME_MOUNT_PATH" becomes available to output the path. Maybe in django you could use it too?
Brody
Brody13mo ago
you can but it would just equal /media
x4zx
x4zx13mo ago
For some reason in this script, everything worked and brought up the available media files. But django doesn't want to get anything from it at all
Brody
Brody13mo ago
have you tried to comment out that if debug thing
x4zx
x4zx13mo ago
I deleted it completely. I created a separate branch for experiments to run
Brody
Brody13mo ago
show me the new code?
x4zx
x4zx13mo ago
https://github.com/x4zx/lyceum208/tree/test | also, invited you to the repository
Brody
Brody13mo ago
what branch are you working in
x4zx
x4zx13mo ago
As of now, the test branch
Brody
Brody13mo ago
I don't see anything in urls.py that lets django know to load files from media root
x4zx
x4zx13mo ago
You specify the path to the files in the settings. In production mode, you do not need to specify anything in urls.py
Brody
Brody13mo ago
okay well you clearly need to specify something extra somewhere, so I don't know why you are so opposed to trying what I have suggested
x4zx
x4zx13mo ago
I'm not against it, I'm just saying that it doesn't help in production.
Brody
Brody13mo ago
says who
x4zx
x4zx13mo ago
Many other developers. if settings.DEBUG: is used only during development, so that django understands where the files come from. In production, a special server for media files is responsible for this
Brody
Brody13mo ago
okay so where's this special server you speak of
x4zx
x4zx13mo ago
Well, since you have "volume", it turns out that the railway is a special server for storing media files
Brody
Brody13mo ago
that sentence does not make any sense you need to configure django to serve the files from the volume
x4zx
x4zx13mo ago
I wish I knew how to do it.
Brody
Brody13mo ago
so far you've only shown me code that tells django where to save the file, but nothing that tells django where to access the saved files
Ray
Ray13mo ago
Have you tried uploading another file after changing the root to /media? Previously uploaded files wouldn’t be in the volume
x4zx
x4zx13mo ago
Yeah, but it didn't do anything. The file is still "not found". I tried to run the site locally with DEBUG=False and the error is the same, "file not found".
x4zx
x4zx13mo ago
Although there is a file in the media folder
x4zx
x4zx13mo ago
Ray
Ray13mo ago
Ya but that’s your localhost, not production. Try changing MEDIA_URL to /media/, MEDIA_ROOT to /media Brody is right, you want to tell Django how to serve files from the volume where they’re stored. Storage is different from serving Google for: * django serve media from path * django media static url production
x4zx
x4zx13mo ago
Changed it, but it didn't help
x4zx
x4zx13mo ago
By the way, with this value, when I change the avatar in the profile, the folder media is created in the root of the drive
x4zx
x4zx13mo ago
Ray
Ray13mo ago
That’s what you want in production. Remove the BASE_DIR If you want separate settings, do what you did in settings.py with if settings.DEBUG