[英]How can I load my own/custom CSS - Django
我正在尝试在 Django 中加载我自己的自定义 CSS,但没有成功。 奇怪的是主 CSS (style.css) 加载正确。
这是我的 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' %}">
我的设置:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
STATIC_DIR,
]
我的 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)
有什么帮助吗?
谢谢!
它不起作用,因为您没有指定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,
]
您的自定义 css 在哪里? 在哪个静态文件夹中?
在css/style.css
旁边的项目级static
文件夹中? 在这种情况下,它应该可以正常加载,也许你的文件名有错别字?
在应用程序static
文件夹中? 在这种情况下,我想你需要添加
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
参考https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-STATICFILES_FINDERS
也许您需要重新运行以下命令:
python manage.py collectstatic
因此可以在下次重新加载时考虑新资产。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.