#How do I get the volume files right in django?

1 messages · Page 1 of 1 (latest)

proven viper
#

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?

candid stagBOT
#

Project ID: 6a37fcf0-b45a-43ea-aec7-9aac046bdf7f

proven viper
#

6a37fcf0-b45a-43ea-aec7-9aac046bdf7f

proven viper
tawny anchor
#

does MEDIA_ROOT tell django where to place the uploaded files?

proven viper
tawny anchor
#

well if you mounted the volume to /media then /media would be your media root

#

but you have prefixed it with the base dir

proven viper
tawny anchor
#

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

proven viper
#

So, I did as you said. Now let's see if it works.

tawny anchor
#

thats not at all what i said lol

#

remove BASE_DIR!! BASE_DIR is not the volume mount!!

proven viper
tawny anchor
#

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

proven viper
#

For some reason it still doesn't find

#

Is there any way to see what's in volume right now?

tawny anchor
#

theres not

#

volumes is still beta after all

#

have you uploaded that file first?

proven viper
proven viper
tawny anchor
#

do you have the correct config in your urls.py?

proven viper
tawny anchor
#

if settings debug

#

what do you have debug set to

proven viper
tawny anchor
#

then those extra urlpatterns arent getting added?

proven viper
tawny anchor
#

that could be a problem?

proven viper
#

Honestly, I have no idea.

tawny anchor
#

remove if statement?

proven viper
tawny anchor
#

why would you want it to not work?

proven viper
# tawny anchor why would you want it to not work?

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)

That is, when DEBUG=True

tawny anchor
#

but is it? what if it is needed in production

proven viper
#

You can basically delete this bit of code

tawny anchor
#

how can you be sure though? it isn't working right now

proven viper
tawny anchor
#

would it hurt to try and remove that check

proven viper
tawny anchor
#

well are there any error logs in the logs?

tawny anchor
#

just seems like django isn't setup properly to read the files back out of the media volume

proven viper
tawny anchor
#

media root doesn't have a forward slash prefixing it

#

probably not the core issue but still

proven viper
#

Should I delete the media folder I created?

tawny anchor
#

that wouldn't interfere with anything, since the volume mounts to /media relative to the root of the container

proven viper
tawny anchor
#

I'm sure there is, but that would be a question for google

proven viper
#

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?

tawny anchor
#

you can but it would just equal /media

proven viper
tawny anchor
#

have you tried to comment out that if debug thing

proven viper
tawny anchor
#

show me the new code?

proven viper
tawny anchor
#

what branch are you working in

proven viper
tawny anchor
#

I don't see anything in urls.py that lets django know to load files from media root

proven viper
#

You specify the path to the files in the settings. In production mode, you do not need to specify anything in urls.py

tawny anchor
#

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

proven viper
tawny anchor
#

says who

proven viper
#

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

tawny anchor
#

okay so where's this special server you speak of

proven viper
tawny anchor
#

that sentence does not make any sense

#

you need to configure django to serve the files from the volume

proven viper
tawny anchor
#

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

neat mango
#

Have you tried uploading another file after changing the root to /media? Previously uploaded files wouldn’t be in the volume

proven viper
proven viper
neat mango
#

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
proven viper
proven viper
neat mango
#

That’s what you want in production. Remove the BASE_DIR