每次開新的 Django project 都會做的設定

每次開新的 Django project 時, 都得重覆下面這些事情, 其實有點惱人。

首先是 settings.py, 我會加入下面這一段

import os  
ROOT_PATH = os.path.dirname(__file__)  

然後修改 MEDIAROOT 跟 TEMPLATEDIRS 使用相對路徑

MEDIA_ROOT = os.path.join(ROOT_PATH, 'static')

TEMPLATE_DIRS = (  
    os.path.join(ROOT_PATH, 'templates')
)

設定 MEDIA_URL

MEDIA_URL = '/static/'  

最後我還會加上下面這一段
TEMPLATECONTEXTPROCESSORS = (
‘django.core.contextprocessors.auth’,
‘django.core.context
processors.debug’,
‘django.core.contextprocessors.i18n’,
‘django.core.context
processors.media’,
‘django.core.context_processors.request’,
)

再來是 urls.py

from django.conf import settings  

先 import 設定檔, 然後加入下面這一段讓 development server 可以吐靜態檔

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

大概是這樣~

tzangms

Read more posts by this author.

Subscribe to Oceanic / 海海人生

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!