Django Dir peizhi - WBowam/wbowam.github.com GitHub Wiki

Date: 2014-05-14 Title: Django Dir 配置(方法一) Tags: Django Category: It

Env:
  • django==1.4.5
  • python==2.7
  • ubuntu==13.04

公用部分

settings.py

##added by Tulpar,20140514
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))

MEDIA

settings.py

MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'media/')
MEDIA_URL = '/media/'

urls.py

##added by Tulpar,20140514
from django.conf import settings

urlpatterns += patterns('',
    url(r"^media/(?P<path>.*)$","django.views.static.serve",{"document_root": settings.MEDIA_ROOT,}),
)

STATIC

Settings.py

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    #os.path.join(PROJECT_ROOT, 'static/'),
    )

url.py

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

TEMPLATE

settings.py

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates/'),
)

发现django 1.8之后 配置方法发生了改变
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'APP_DIRS': True,
        'DIRS': ['templates'],

    },
]
⚠️ **GitHub.com Fallback** ⚠️