0

I'm building a project in Django 3, but the images aren't linking in the files.

All images are store in a folder called media & here's the HTML for my logo for example -

<a class="navbar-brand" href="{% url 'index' %}">
<img src="{{ MEDIA_URL}}logo.png" height="70" class="pr-4" alt="Site logo">

Then I've this in my settings -

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

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

and in my project level URLS I've got -

from django.conf import settings
from django.conf.urls.static import static

+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Claire
  • 57
  • 6

1 Answers1

0

You should use static folder for the static content like images, css, js etc... and media for the dynamic content like uploads from users, avatar pictures... then you should use this template tag:

{% load static %}

<a class="navbar-brand" href="{% url 'index' %}">
<img src="{% static 'logo.png' %}" height="70" class="pr-4" alt="Site logo">

If you want to use media folder anyways, you need to add this in your urls.py (extracted from here) in order to make it work fine in local:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = patterns('',
    # ... the rest of your URLconf goes here ...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
NoiK
  • 162
  • 1
  • 11
  • 1
    Sweet as! Changed it all to the static folder & it's working now. Not too sure why the {{MEDIA_URL}} stuff wasn't as that's exactly how I had it linked, just outside of my URL patterns, but I'll cross that bridge when I start that section! lol – Claire Jun 09 '20 at 22:52