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