我的 CSS 文件位于 /ecomstore/static/css.css
我已经将 css 链接到 head 标签中的 base.html
<link rel="Stylesheet" type="text/css" href="/static/css.css" />
我的 urls.py 文件:
from django.conf.urls import include, url
from django.contrib import admin
from ecomstore import settings
from django.contrib.staticfiles import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^catalog/','preview.views.home'),
# url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
# { 'document_root' : '/home/yogesh/ecomstore/static' })
]
urlpatterns = [
# other commented code here
url(r'^catalog/?','preview.views.home'),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve',{ 'document_root' : '/home/yogesh/ecomstore/static/' }),
]
设置.py文件
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 'DIRS': [os.path.join(CURRENT_PATH, 'templates')],
'DIRS': [os.path.join(BASE_DIR, 'templates/')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
静态设置:
STATIC_URL = '/static/'
STATIC_ROOT = "/home/yogesh/ecomstore/static/"
尽管有这些东西,我还是不知道为什么我的 CSS 模板没有加载。同样在终端中,我收到以下指示某种错误的信息。
[2015 年 11 月 24 日 11:11:36] "GET/static/css.css HTTP/1.1"404 1735
最佳答案
使用
{% static %}
模板标签:{% load staticfiles %}
在您的设置中配置静态文件目录,不要在您的 urlconf 中硬编码路径:
STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"), '/var/www/static/', )
确保您在开发期间提供静态文件:
urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
检查静态文件目录
/home/yogesh/ecomstore/static/
是否可由您的 django 进程读取。
关于python - Django 1.8 无法加载 css 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892157/