【发布时间】:2014-07-30 13:44:01
【问题描述】:
我能够将我的静态文件 (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。感谢所有帮助,谢谢。
【问题讨论】:
-
我只是尝试设置我的
STATIC_ROOT = /static/并运行python manage.py collectstatic,这似乎没有任何改变。 -
您是否设置了服务器来提供静态文件? STATIC_ROOT 也应该指向完整路径,/static/ 表示您从根文件夹静态提供服务。
-
您需要设置服务器以提供静态文件。它不是由 Django 在生产中处理的。见docs.djangoproject.com/en/dev/howto/static-files/#deployment
-
我喜欢这篇关于服务静态的帖子:hasnath.net/blog/…
标签: python css django django-staticfiles static-files