无法在Django中加载CSS文件

时间:2013-03-12 18:17:51

标签: css django django-settings

我正在使用Ubuntu和Django 1.3.7。以下是各种相关设置:

我的目录结构:

mysite/
      manage.py
      settings.py
      __init__.py
      urls.py
      myapp/
          __init__.py
          forms.py
          models.py
          views.py
          templates/
               index.html
               home.html
      static/
           common.css
           ...
           ...

我的settings.py文件:

import os.path
SETTINGS_ROOT = os.path.dirname(__file__)
INSTALLED_APPS = (
    ...
    ...
       'django.contrib.staticfiles'
    )
    STATIC_ROOT = os.path.join(SETTINGS_ROOT, "static/")
    STATIC_URL = '/static/'

在我的模板html文件中:

<link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap.css">

但是,这些页面不显示任何图像,css或js文件。我已经阅读了文档。但没有任何帮助。为了让文件运行,还需要包含/修改什么?

3 个答案:

答案 0 :(得分:0)

STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    '/path/to/your/project/assets',
)

答案 1 :(得分:0)

<强> settings.py

import os
SETTINGS_ROOT = os.path.dirname(__file__)

STATIC_ROOT = os.path.join(SETTINGS_ROOT, "static")
STATIC_URL = '/static/'

STATICFILES_DIRS = (

    os.path.join(SETTINGS_ROOT, 'static'),
)

urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

admin.autodiscover()

urlpatterns = patterns('',
    ................
) 

urlpatterns += staticfiles_urlpatterns()

<强>模板

<link rel="stylesheet" type="text/css" href="/static/bootstrap/css/bootstrap.css">

答案 2 :(得分:0)

我的静态文件配置如下。

PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
enter code herePER_DIR = os.path.abspath(os.path.join(PROJECT_DIR, os.path.pardir))
STATIC_ROOT = os.path.join(SUPER_DIR, 'static')
STATIC_URL = '/static/'

它可能会有所帮助