简体   繁体   English

Django - 如何处理协作项目中settings.py中的路径

[英]Django - How to deal with the paths in settings.py on collaborative projects

I have just started a feasibility study on Django for my company and I have noticed the need for absolute paths on settings.py: 我刚刚为我的公司开始了关于Django的可行性研究,我注意到settings.py上需要绝对路径:

TEMPLATE_DIRS = (
   # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
   # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

My question is: How to deal with these absolute path when you're collaborating with a team? 我的问题是:当你与团队合作时,如何处理这些绝对的路径? Say, if a team member has to modify the paths after getting the project from source control not only will this be error prone and time wasteful but it will also cause complications when this user has to commit a change made to settings.py. 比如说,如果团队成员在从源代码控制获取项目后必须修改路径,那么这不仅会出错,而且浪费时间,但是当用户必须对settings.py进行更改时,这也会导致复杂化。 How can I avoid this? 我怎么能避免这个?

import os.path

#Get the absolute path of the settings.py file's directory
PWD = os.path.dirname(os.path.realpath(__file__ )) 

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or 
    # "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.

    #Add Templates to the absolute directory
    os.path.join(PWD, "Templates") 
)

That's how I do relative imports. 这就是我做相对进口的方式。 Note that is usually wise to have a separate localsettings.py file, or something similar. 请注意,通常明智的做法是使用单独的localsettings.py文件或类似文件。

Do this: 做这个:

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

This will set the paths according to the directory of settings.py file 这将根据settings.py文件的目录设置路径

settings.py is just another Python module. settings.py只是另一个Python模块。 Import and use the various functions in os.path to build your paths. 导入并使用os.path的各种函数来构建路径。

The alternative to using relative path from the settings.py file, is for each developer to have their own settings.py. 使用settings.py文件中的相对路径的替代方法是让每个开发人员拥有自己的settings.py。

# settings.py
TEMPLATE_DIRS = (
    'c:\django\templates\',
)

# dev-x-settings.py
import settings
TEMPLATE_DIRS = (
    'd:\dev\django\project\templates\'
)

The dev-x-settings.py imports all the settings from the base file, and changes the bits and pieces they need to. dev-x-settings.py从基本文件导入所有设置,并更改它们所需的位和部分。 Also very handy for maintaining a local sqlite database and the like. 同样非常方便维护本地sqlite数据库等。

We usually set out our settings as: 我们通常将设置设置为:

/settings/
    __init__.py
    production.py
    staging.py
    test.py
    dev-x.py
    dev-y.py

Then all you need to worry about is running the server and passing it the correct settings.py file. 然后,您需要担心的是运行服务器并向其传递正确的settings.py文件。

Besides using os.path you could add 除了使用os.path你可以添加

try:
    import * from settings_local
except:
    pass

at the end of you settings.py . settings.py的末尾。 Every developer can then create his own settings_local.py file which is not checked in into the VCS! 然后,每个开发人员都可以创建自己的settings_local.py文件,该文件未签入VCS!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM