Felipe Dourado
Felipe Dourado

Reputation: 441

How can I load my own/custom CSS - Django

I'm trying to load my own customized CSS in Django but with no success. The weird thing is that the main CSS (style.css) is loading correctly.

Here is my base.html:

<!-- Main Style CSS -->
<link rel="stylesheet" href="{% static 'css/style.css' %}">

<!-- My Own Style CSS -->
<link rel="stylesheet" href="{% static 'css/custom.css' %}">

My settings:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'

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

My urls.py:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('cart/', include('cart.urls')),
    path('payment/', include('payment.urls')),
    path('orders/', include('orders.urls')),
    # path('users/', include('django.contrib.auth.urls')),
    path('', include('django.contrib.auth.urls')),
    path('', include('account.urls')),
    path('', include('dma.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + \
              static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Any help?

Thank you!

Upvotes: 0

Views: 133

Answers (2)

cizario
cizario

Reputation: 4254

where is located your custom css ? in which static folder ?

  • in project level static folder next to css/style.css ? in this case it should be loaded with no problem maybe you have a typo in the file name ?

  • in app static folder ? in this case, i guess you need to add

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)

refer to https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-STATICFILES_FINDERS

Update

maybe you need to re-run the command below:

python manage.py collectstatic

so the new assets can be taken in consideration the next reload.

Upvotes: 0

Aymane Talibi
Aymane Talibi

Reputation: 11

It won't work because you didn't specify a MEDIA_URL, so try this:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
    
STATIC_URL = '/static/'
MEDIA_URL = '/css/'
    
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
    STATIC_DIR,
]

Upvotes: 1

Related Questions