Staticfiles not loading on Django

Viewed 25

My static files are not loading on Django. I'm running this on my local machine. The location of example.png is main/static/main/example.png. main is an app.

Here's my settings.py:

from pathlib import Path
import os

BASE_DIR = Path(__file__).resolve().parent.parent
DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'tutorials.urls'
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR, ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'tutorials.wsgi.application'

...

STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

My template, index.html:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
</head>
<body>
    <nav>
        <div class="logo">
            Tutorials.
        </div>
    </nav>
    <img src="{% static 'main/example.png' %}" alt="My image">
</body>
</html>

The static files called for in index.html do not load. The alt text is seen.

2 Answers

First of all I think you have not configured your static files and media files. Try configuring it as follows. In your settings.py ensure to include STATICFILES_DIR, STATIC_ROOT, MEDIA_URL, MEDIA_ROOT in your settings.py and then add the below lines below STATIC_URL = 'static/'

 MEDIA_URL = 'media/'
 STATICFILES_DIRS = [BASE_DIR / 'static']
 STATIC_ROOT = BASE_DIR / 'staticfiles/'
 MEDIA_ROOT = BASE_DIR / 'static/media'
 

By doing this you are telling django where to get your static files. now your have to add the link of the static files in your project urls.py. you will add that as below.

 from django.conf import settings
 from django.conf.urls.static import 
 static
 
 urlpatterns = [
      .......
    ]
 urlpatterns += 
 static(settings.STATIC_URL, 
 document_root=settings.STATIC_ROOT)
 urlpatterns += static(settings.MEDIA_URL, 
 document_root=settings.MEDIA_ROOT)

One last thing that I assumed you had done is creating static folder in your project root and inside that static folder create media folder where all the images you want to load are. Now run python manage.py collectstatic

Follow this

install:

pip install whitenoise

then add this on top of the installed apps.

'whitenoise.runserver_nostatic', 

Add these lines in settings.py

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / "static"
MEDIA_ROOT = BASE_DIR / "media"
MEDIA_URL = '/media/'

Add these lines in project urls.py after the urlpatterns where settings.py located.

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Now static files will load.

For more information, follow the whitenoise documentation

Related