0
votes

I don't know why django can't load static files even though I have defined static_url, static_root, staticfiles_dirs and urlpatterns including static_root. I put static files in root_dir/static/ directory.

My templates also got right syntax with {% load static%} and {% static '...'%}

Please give me some advice on this.

Many thanks

This is my main urls file

from django.contrib import admin
from django.urls import path
from home import views as HomeViews
from django.conf import settings 
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
    path('',HomeViews.index,name='index')
]

if settings.DEBUG:
    urlpatterns = urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
1
kindly share the main url codes - Kenneth Githambo
hey @KennethGithambo , I have added urls,py file, please have a look - Knight

1 Answers

0
votes

check if your program has the following :

settings.py :

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_DIR = os.path.join(BASE_DIR,"static")


 STATIC_URL = '/static/'
STATICFILES_DIRS = [
    STATIC_DIR,
]

dont forget to load this tag in your template file

{% load static %}

check for the call syntax

<link rel="stylesheet" href="{% static 'base.css' %}">

check if you have created the static folder in the base directory ( where you have your manage.py file)

Check if the contents in the static file and the call corresponds with each other (check spellings)

once try to make migrations and migrate

still if not working .... Try hard refresh (ctrl + F5)