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

2
  • 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. Commented Jun 26, 2020 at 18:17
  • Yes, 'django.contrib.staticfiles', is there in the INSTALLED_APPS list. Commented Jun 26, 2020 at 18:32

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

8 Comments

Is this the same as declaring the STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') in the sttings.py?
I've tried that, and I just tried it again, but that doesn't fix the issue.
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?
@b0302 I edited the answer again included a import see it
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.