Django1.6 静态目录的配置

Python 2013-12-12 Django,static,静态目录

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

"""
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/'

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

网上搜索相关答案,大多是让你配置一个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}),
)

启动服务访问静态文件还是404page。

后来,不断尝试发现在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

如果你要访问D盘的静态文件,直接输入文件名,程序会索引D盘下的静态文件,加入访问D盘根目录下的b.txt文件:

如果你的D盘下跟你的MEDIA_ROOT同样也有一个a.txt,那么它会根据STATICFILES_DIRS中定义的目录顺序依次查找,优先找到谁就用谁,如D盘下也有一个a.txt,内容如下:

这个a.txt是在D盘根目录下

然后访问a.txt,看看:

它优先索引了MEDIA_ROOT下的a.txt,要是把STATICFILES_DIRS里定义的目录换一下位置:

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

在访问a.txt,那么就是D盘有限索引了

开发环境可以这么干,如果拿到线上,还是把static的东西交给nginx这种专业的webserver来做吧!

文字链接:《Django1.6 静态目录的配置

文章地址:http://www.qttc.net/201312390.html

除非标注,琼台博客所有博文均为原创,转载请加文字链接注明来源

乳名?小名?昵称?网名?均可

email,放心,我不会给你乱投广告的

想获得回访就把你的站点URL写上(没有留空)

[NOTICE]木要投放广告
[NOTICE]木要骂人,说不该说的话
[NOTICE]自由言论,但要遵纪守法

Comments 0

    Hi,你想第一个做沙发么?