0

EDIT: I figured out that the CSS file was not being displayed because I copied the link tag pointing to the CSS file from Microsoft Word, so the quotations marks around rel="stylesheet" were not the right type of quotations marks!!

The settings.py file in the project folder defines the STATIC_URL variable as '/static/'

The CSS file is inside the app folder: DjangoProject/shop/static/shop/style.css

The {% load static %} is at the top of the HTML template (if I use "staticfiles" instead of "static", I get an error)

The CSS file is linked using href="{% static 'shop/style.css' %}"

The CSS file loads and is visible when I analyze the "view page source", but the actual CSS is not displayed on the webpage.

What am I doing wrong? I'm using Django version 3.0.7

EDIT:

I've also tried defining the STATICFILES_DIRS in the settings.py file, but that doesn't do anything. Nor does adding the additional code below to the project urls.py file.

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

.

from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    #your url patterns
]
urlpatterns += staticfiles_urlpatterns() 

Image of my code

f0302
  • 33
  • 5
  • Do you have django.contrib.staticfiles inside of your `INSTALLED_APPS` in settings.py? It would be more beneficial if you included your actual code in stead of an image. – plum 0 Jun 26 '20 at 18:17
  • Yes, 'django.contrib.staticfiles', is there in the INSTALLED_APPS list. – f0302 Jun 26 '20 at 18:32

1 Answers1

1

you have to add

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

in your settings.py file below STATIC_URL

add bolew code in main urls.py

from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

urlpatterns = [
    #your url patterns
]
urlpatterns += staticfiles_urlpatterns()

and make sure you are hard refreshing the webpage and try restarting server

Pavan kumar
  • 478
  • 3
  • 16
  • Is this the same as declaring the `STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')` in the sttings.py? – plum 0 Jun 26 '20 at 18:12
  • I've tried that, and I just tried it again, but that doesn't fix the issue. – f0302 Jun 26 '20 at 18:14
  • I've tried adding something similar to the urls.py file before, but I always get an error. With the code you provided, I get "NameError: name 'staticfiles_urlpatterns' is not defined". How can I fix this NameError? – f0302 Jun 26 '20 at 18:22
  • no it's not same @plum0 https://stackoverflow.com/a/24022604/11602126 – Pavan kumar Jun 26 '20 at 18:23
  • @b0302 I edited the answer again included a import see it – Pavan kumar Jun 26 '20 at 18:24
  • if need any further help just comment – Pavan kumar Jun 26 '20 at 18:25
  • The import of staticfiles_urlpatterns fixes the NameError, but it still does not display the CSS, even after restarting the server :\ – f0302 Jun 26 '20 at 18:27
  • @Pavankumar Oh, that post helped me so much understand the difference between these settings! Thank you! The django documentation on it got me a bit lost but that person laid it out in a way I could comprehend. – plum 0 Jun 26 '20 at 18:27