Django Static Files not loaded in template but they are there

Viewed 61

i am new to Django and web developement and am struggling with Static Files. I have seen this or similar questions in here before, but nothing seemed to bring me a solution. The Problem is: I can find the static files in Django, but they are not loaded within the template.

My Folder Structure is as follows (everything in a folder named Django):

media_root_folder
static_root_folder
Project
   Project
   manage.py
   mysite
      __init__.py
      asgi.py
      settings.py
      urls.py
      wsgi.py
   templates
   static
      admin
      Project
         style.css

In Settings.py I have the following:

DEBUG = True    
STATIC_URL = 'static/'
STATIC_ROOT = os.path.abspath(r'E:\LM\Django\static_root_folder')
STATICFILES_DIRS = [
    BASE_DIR / "static"
]    
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
 )

'django.contrib.staticfiles' is added as well as Project to INSTALLED_APPS

I also even added this (I found in a Django Forum that this sometimes solves the Problem):

import mimetypes

mimetypes.add_type("text/css",".css", True)

Within urls.py i accordingly added

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Within my tempalte I am using:

{% load static %}
<link rel="stylesheet" type="text/css" href=" {% static 'Project/style.css' %} ">

If I run manage.py findstatic it finds the files and I also can open them directly in the browser. However if I manage.py runserver and load my site (even in incognito mode and after clearing cache and also in multiple browsers) the CSS is not used for the page at all. I only can see the plain html.

If I load the django/admin side the css is loaded and presented clearly.

What am I doing wrong and how can this be fixed?

2 Answers

The Problem was, that in every template I was extending the base.html . When I was loading the CSS in base.html it worked without any problems.

Related