简体   繁体   English

如何加载我自己的/自定义 CSS - Django

[英]How can I load my own/custom CSS - Django

I'm trying to load my own customized CSS in Django but with no success.我正在尝试在 Django 中加载我自己的自定义 CSS,但没有成功。 The weird thing is that the main CSS (style.css) is loading correctly.奇怪的是主 CSS (style.css) 加载正确。

Here is my base.html:这是我的 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:我的 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!谢谢!

It won't work because you didn't specify a MEDIA_URL , so try this:它不起作用,因为您没有指定MEDIA_URL ,所以试试这个:

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,
]

where is located your custom css ?您的自定义 css 在哪里? in which static folder ?在哪个静态文件夹中?

  • in project level static folder next to css/style.css ?css/style.css旁边的项目级static文件夹中? in this case it should be loaded with no problem maybe you have a typo in the file name ?在这种情况下,它应该可以正常加载,也许你的文件名有错别字?

  • in app static folder ?应用程序static文件夹中? 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参考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.因此可以在下次重新加载时考虑新资产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM