python - Django 1.8 无法加载 css 模板

标签 python html css django django-1.8

我的 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

最佳答案

  1. 使用{% static %}模板标签:

    {% load staticfiles %} 
    

  2. 在您的设置中配置静态文件目录,不要在您的 urlconf 中硬编码路径:

    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, "static"),
        '/var/www/static/',
    ) 
    
  3. 确保您在开发期间提供静态文件:

    urlpatterns = [
         # ... the rest of your URLconf goes here ...
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    
  4. 检查静态文件目录 /home/yogesh/ecomstore/static/ 是否可由您的 django 进程读取。

关于python - Django 1.8 无法加载 css 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33892157/

相关文章:

python - heroku 上的 Gunicorn 'ImportError: No module named app.wsgiapp'

python - PySpark 2.2.0 : 'numpy.ndarray' object has no attribute 'indices'

php - 防止 PHP 在 POST 之前运行

php - 在MySQL数据库中指定HTML输入属性?

javascript - 如何根据屏幕尺寸更改 slider 和表单高度

javascript - 拉维尔 4 : Sidebar is not clickable

css - 浏览器渲染时隐藏html标签

python - 如何从弹性滚动结果更快地建立 Pandas 数据框?

css - <hr/> 标签应根据文本动态增加大小

python - 使用行和列多重索引和给定顺序从表格转换为矩阵