Django静态文件在本地加载,但不在开发服务器上加载

时间:2014-06-09 22:31:47

标签: python css django django-staticfiles static-files

我能够在我的本地开发环境中加载我的静态文件(CSS),但是当我将更改推送到我的开发服务器时,我无法加载CSS。我的本地环境是Mac OS 10.9.2,我的开发服务器运行的是Ubuntu 12.04.4 x64。

settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
  os.path.join(BASE_DIR, 'static/'),
)

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#7h&rny3^hz&q6w-8$6k&+msh554$pz*tx@$lj(+dgctvuj2j%'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

AUTHENTICATION_BACKENDS = (

    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',

)

TEMPLATE_CONTEXT_PROCESSORS = (

    'django.core.context_processors.request',
    'allauth.account.context_processors.account',
    'allauth.socialaccount.context_processors.socialaccount',
    'django.contrib.auth.context_processors.auth',

)

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'pcatapp',
    'south',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'tastypie',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    #'django.core.context_processors.csrf',
)

ROOT_URLCONF = 'pcat.urls'

WSGI_APPLICATION = 'pcat.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'db',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

SITE_ID = 1

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/' 

摘自页面来源:

<link rel="stylesheet" href="/static/myapp/style.css">

我的静态文件夹位于myapp/static/myapp/style.css。感谢所有帮助。

2 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,dj-static提供了一个简单的解决方案

$ pip install dj-static

在你的wsgi.py文件中:

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

祝你好运

答案 1 :(得分:1)

当使用djangos开发服务器时,我发现这个问题及其解决方案通常位于URLS文件中(当DEBUG = True时),这样的事情将允许你使用django来提供静态文件:

from django.conf import settings
from django.conf.urls.static import static


urlpatterns = patterns(
    '',
    ... url includes etc.
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

这意味着任何来自{{STATIC_URL}}的网址都将由django处理,以下是https://docs.djangoproject.com/en/dev/howto/static-files/

上的django文档

编辑(添加了在网络服务器下运行而不是django开发服务器的解决方案):

如果您在Web服务器后面运行项目,而不是使用djangos runserver,那么最好不要使用djangos静态文件查找器,而是使用其静态文件收集器和Web服务器来提供静态文件,为此,在settings.py中试试这个:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

然后运行./manage.py collectstatic并确保您的网络服务器将请求从/static/重定向到您的静态文件目录的完整路径,例如在NGINX中您可能会这样做。

location /static/ {
    alias   /var/www/myproject/static/;
}

和阿帕奇:

Alias /static/ /var/www/myproject/static/

希望这会有所帮助......