2015-08-20 123 views
2

項目總監這是我的項目樹:路徑在Django

projectname 
    projectname 
     init.py 
     settings.py 
     urls.py 
     wsgi.py 
    appname 
     init.py 
     admin.py 
     models.py 
     test.py 
     views.py 
     urls.py 
    templates 
     base.html 
     login.html 

現在在settings.py我使用這個代碼:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(BASE_DIR), "projectname", "templates"), 
    ) 

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "projectname", "static", "static-only") 
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "projectname", "static", "media") 

我怎樣才能得到的路徑項目目錄,這樣我就不需要在代碼中輸入項目名稱projectname,並在其他任何django項目中使用該代碼?

更新

或者我能不能用這個

BASE_DIR+'/templates' 
BASE_DIR+'/static/media' 

或者是一個壞主意?

+0

你可以只放在一個directories.py文件的線條和導入邏輯的settings.py,那麼你就可以重用settings.py,只是更改目錄。 py爲不同的django項目。 – jwde

+0

是的,我想我可以嗎?但那是更多的工作。 :D或者,我可以用'BASE_DIR +'/ templates''作爲路徑嗎? – Kakar

回答

0

BASE_DIR 已經包括 「項目名稱」。當你做os.path.dirname(BASE_DIR),你去up一個級別projectname;只是將它添加回來。不要這樣做。

相反,只需使用BASE_DIR直接:

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, "templates"), 
) 
1

我會建議你使用os.path.abspath

# Project root is intended to be used when building paths, 
# e.g. ``os.path.join(PROJECT_ROOT, 'relative/path')``. 
PROJECT_ROOT = os.path.abspath(os.path.dirname(__name__)) 

# Absolute path to the directory where ``collectstatic`` 
# will collect static files for deployment. 
# 
# For more information on ``STATIC_ROOT``, visit 
# https://docs.djangoproject.com/en/1.8/ref/settings/#static-root 
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/') 

# Absolute path to the directory that will hold uploaded files. 
# 
# For more information on ``MEDIA_ROOT``, visit 
# https://docs.djangoproject.com/en/1.8/ref/settings/#media-root 
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'uploads/')