[英]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.