Django1.6静态目录的配置

Django1.6版本已经发布,昨日下载后在配置静态目录时总是出问题,默认配置如下:

"""
Django settings for daybaby project.
 
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
 
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
 
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
 
 
# 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 = '0j9!$d5nc3i2xiso2(bo-9!mkjy7j3%to9r$)0a!kru0d2mr6e'
 
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
 
TEMPLATE_DEBUG = True
 
ALLOWED_HOSTS = []
 
 
# Application definition
 
INSTALLED_APPS = (
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
)
 
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',
)
 
ROOT_URLCONF = 'daybaby.urls'
 
WSGI_APPLICATION = 'daybaby.wsgi.application'
 
 
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
 
DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
  }
}
 
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
 
LANGUAGE_CODE = 'zh-cn'
 
TIME_ZONE = 'Asia/Shanghai'
 
USE_I18N = True
 
USE_L10N = True
 
USE_TZ = True
 
 
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
 
STATIC_URL = '/static/'

【国内直连ChatGPT 29元起】
国内直连ChatGPT,Plus会员每月29元起,支持最新o1模型探索更多领域,无需注册OpenAI账号。

settings默认指定了一个STATIC_URL,但你在项目下创建static目录却还是不能访问

D:\Workspaces\Aptana\daybaby>tree /F
文件夹 PATH 列表
卷序列号为 4603-09B2
D:.
│  .project
│  .pydevproject
│  manage.py
│
├─daybaby
│  │  settings.py
│  │  settings.pyc
│  │  urls.py
│  │  urls.pyc
│  │  view.py
│  │  view.pyc
│  │  wsgi.py
│  │  wsgi.pyc
│  │  __init__.py
│  │  __init__.pyc
│  │
│  └─__pycache__
│          settings.cpython-34.pyc
│          urls.cpython-34.pyc
│          view.cpython-34.pyc
│          wsgi.cpython-34.pyc
│          __init__.cpython-34.pyc
│
├─media
│      a.jpg
│
├─static
│      a.jpg
│
└─templates
        index.html

访问static下的a.jpg提示404

full

网上搜索相关答案,大多是让你配置一个STATIC_ROOT:

STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL.replace("/", ""))

然后在urls.py里再加上:

from daybaby import settings
 
urlpatterns += patterns('',
  (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)

重试还是404。后来,不断尝试发现在Django1.6中配置静态文件是很容易的,首先把urls.py中的

from daybaby import settings
 
urlpatterns += patterns('',
  (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)

删除它,然后在settings.py里定义一个MEDIA_ROOT

MEDIA_ROOT = os.path.join(BASE_DIR, STATIC_URL.replace("/", ""))

接着定义一个STATICFILES_DIRS,这是一个tuple,把你所有静态目录都push到这里,当你访问一个静态文件时它会按照这个目录优先级去找,如

STATICFILES_DIRS = (
  MEDIA_ROOT,
  "D:\\",
)

然后,如果你有定义了STATIC_ROOT,请确保它的值是唯一的,否则启动会报:

django.core.exceptions.ImproperlyConfigured: The MEDIA_ROOT and STATIC_ROOT settings must have different values

这个值也没用了,所以你可以在你settings.py里把STATIC_ROOT改下值,或者删除都可以,那不是必须的,必须要求的是MEDIA_ROOT的定义,只有定义它你的静态目录才管用。

以上,我添加了两个目录,一个是MEDIA_ROOT,一个是D盘根目录下,下面启动服务,访问MEDIA_ROOT目录下的a.txt成功,它索引文件的优先级根据STATICFILES_DIRS配置的优先级来检索,开发环境可以这么干,如果拿到线上,还是把static的东西交给nginx这种专业的WebServer来做吧。

分享

TITLE: Django1.6静态目录的配置

LINK: https://www.qttc.net/390-django-1-6-static-path-setup.html

NOTE: 原创内容,转载请注明出自琼台博客