pythondjangodjango-templatesdjango-media

Django images don't show up on the web-site


settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

template

<img src="{{ group_avatar.avatar.url }}" style="width:30%" alt="Group Avatar" class="navbar-brand">

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),          # Django admin route
    path("", include("apps.authentication.urls")), # Auth routes - login / register

    # ADD NEW Routes HERE

    # Leave `Home.Urls` as last the last line
    path("", include("apps.home.urls"))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Instead of getting an image, I get an alt text... I've tried to fix it up for a long time, but nothing works...

Django version 3.2.16, using settings 'core.settings'

I suppose that the problem is in the project structure. It was done before I started working on the project and it would be difficult to change it...

application is in the folder apps, which is in the root folder, and template which is needed is also there. But the folder 'media' is in the root directory. Although I'm not sure that the problem is in this.

Interesting fact is that if I manually write the way to the image from static folder (static folder is also localed in apps directory) it will work.

Would be glad for any help)

Searching, reading documentation. I expect a hint how to fix it or where to search the bugyour text


Solution

  • re_path(r'^.*\.*', views.pages, name='pages')
    

    In my urls.py was this line, and after I deleted this line it started to work, I'm not sure how, but it blocked correct work of the links