Django - 如何处理settings.py中协作项目的路径 [英] Django - How to deal with the paths in settings.py on collaborative projects

查看:133
本文介绍了Django - 如何处理settings.py中协作项目的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为我公司开始了Django的可行性研究,我注意到settings.py上需要绝对的路径:

  TEMPLATE_DIRS =(
#将字符串放在这里,如/ home / html / django_templates或C:/ www / django / templates
#即使在Windows上也可以使用正斜杠。
#不要忘记使用绝对路径,而不是相对路径

我的问题是:当您与团队合作时,如何处理这些绝对路径?说,如果一个团队成员在从源代码管理获取项目之后必须修改路径,这样做不仅容易出错,而且浪费时间,否则这个用户必须对settings.py进行更改。如何避免这种情况?

解决方案

  import os.path 

#获取settings.py文件目录的绝对路径
PWD = os.path.dirname(os.path.realpath(__ file__))

TEMPLATE_DIRS =(
#把字符串放在这里,像/ home / html / django_templates或
#C:/ www / django / templates
#总是使用正斜杠,甚至在Windows上
#不要忘记使用绝对路径,而不是相对路径

#Add绝对目录的模板
os.path.join(PWD,模板)

这是我做相对导入的方式。注意,通常明智的做法是单独使用localsettings.py文件,或类似的东西。


I have just started a feasibility study on Django for my company and I have noticed the need for absolute paths on 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. 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.

这篇关于Django - 如何处理settings.py中协作项目的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆