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
Project ID:
6a37fcf0-b45a-43ea-aec7-9aac046bdf7f
6a37fcf0-b45a-43ea-aec7-9aac046bdf7f
The path is correct, but the site can't get the photo
does
MEDIA_ROOT
tell django where to place the uploaded files?Yes, here is a screenshot from
setting.py
. You can also look at the structure of all the files.well if you mounted the volume to
/media
then /media
would be your media root
but you have prefixed it with the base dirMEDIA_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.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
So, I did as you said. Now let's see if it works.
thats not at all what i said lol
remove
BASE_DIR
!! BASE_DIR
is not the volume mount!!so?
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 meFor some reason it still doesn't find
Is there any way to see what's in volume right now?
theres not
volumes is still beta after all
have you uploaded that file first?
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 errordo you have the correct config in your urls.py?
app:
teacher
-> urls.py
if settings debug
what do you have debug set to
False
then those extra urlpatterns arent getting added?
No, they don't.
that could be a problem?
Honestly, I have no idea.
remove if statement?
So it doesn't work when in production mode
why would you want it to not work?
This code is only needed to make static and media files in development mode correctly:
That is, when DEBUG=True
but is it? what if it is needed in production
There is no need for this in production
You can basically delete this bit of code
how can you be sure though? it isn't working right now
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
would it hurt to try and remove that check
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
well are there any error logs in the logs?
no
just seems like django isn't setup properly to read the files back out of the media volume
Maybe, but here is my whole settings.py
media root doesn't have a forward slash prefixing it
probably not the core issue but still
Should I delete the media folder I created?
that wouldn't interfere with anything, since the volume mounts to /media relative to the root of the container
Is it possible to view the contents of /media with the code. Through the same os.listdir()?
I'm sure there is, but that would be a question for google
I plugged "Volume" into the following code and this is what I got
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?
you can but it would just equal
/media
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
have you tried to comment out that if debug thing
I deleted it completely. I created a separate branch for experiments to run
show me the new code?
https://github.com/x4zx/lyceum208/tree/test | also, invited you to the repository
what branch are you working in
As of now, the test branch
I don't see anything in urls.py that lets django know to load files from media root
You specify the path to the files in the settings. In production mode, you do not need to specify anything in urls.py
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
I'm not against it, I'm just saying that it doesn't help in production.
says who
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
okay so where's this special server you speak of
Well, since you have "volume", it turns out that the railway is a special server for storing media files
that sentence does not make any sense
you need to configure django to serve the files from the volume
I wish I knew how to do it.
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
Have you tried uploading another file after changing the root to /media? Previously uploaded files wouldn’t be in the volume
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".
Although there is a file in the media folder
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
Changed it, but it didn't help
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
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