Volumes in Django
imadtoubal
HOBBYOP

a year ago

I have a django app deployed with a Volume mounted on /app/media. When I put hacky code to list the media directory, I can see that the files are there. I can also see that I can upload to the directory via the Django admin.

However, when I access these files via the browser in their django paths, I get 404. Not Found.

Here is my urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path
from django.urls.conf import include

from .views import (contact_success_view, contact_view, game_detail, home,
                    service_detail)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', home, name='home'),
    path('game/<slug:slug>/', game_detail, name='game_detail'),
    path('service/<slug:slug>/', service_detail, name='service_detail'),
    path('ckeditor5/', include('django_ckeditor_5.urls')),
    path('contact/', contact_view, name='contact'),
    path('success/', contact_success_view, name='success'),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And I have in my settings.py:

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
Solved

11 Replies


brody

Hello,Can you try the syntax shown here -https://github.com/railwayapp-templates/django-volume/blob/main/mysite/urls.py#L25

imadtoubal
HOBBYOP

a year ago

That worked! I have been stuck at this all day, thanks a ton!


a year ago

No problem!


Status changed to Solved brody about 1 year ago


romeuriffatti
HOBBY

a year ago

God. I've been stuck in this problem for two days. Thank u guys!


Status changed to Awaiting Railway Response Railway 11 months ago


romeuriffatti
HOBBY

a year ago

Now i have annoter question. Are my uploaded files going to my volume service in this case?


romeuriffatti

Now i have annoter question. Are my uploaded files going to my volume service in this case?

a year ago

If you have a volume attached, and your application is saving the uploaded files to the volume, Yes.


Status changed to Awaiting User Response Railway 11 months ago


romeuriffatti
HOBBY

a year ago

How do i config to my images stay persistant and dont lost them every time a make a commit on github. Sorry for english mistakes, im not native


Status changed to Awaiting Railway Response Railway 11 months ago


romeuriffatti
HOBBY

a year ago

   image = models.ImageField(upload_to='', null=True)     

if this is my uploadto from my model
And

MEDIA_ROOT = BASE_DIR / 'media' MEDIA_URL = 'media/'

this is how i config my settings
Are my files going to the volume or do i need to do something else?


brody

Hello,Can you try the syntax shown here -https://github.com/railwayapp-templates/django-volume/blob/main/mysite/urls.py#L25

a year ago

Have you tried what I have suggested to the owner of this thread?


Status changed to Awaiting User Response Railway 11 months ago


romeuriffatti
HOBBY

a year ago

yes, but thats not how it works. I now resolved the problem. The question is that u can't use the same volume of the database to the application media. I created another one and then it worked.


Status changed to Awaiting Railway Response Railway 11 months ago


a year ago

Yep, media is not stored in the database, it is stored in the filesystem, glad you where able to figure it out!


Status changed to Awaiting User Response Railway 11 months ago


Status changed to Solved brody 11 months ago


Loading...