使用 Fabric 的 INSTALLED_APPS 本地设置
我有一个应用程序(django-compressor),我只想在本地计算机上运行,而不是在服务器上运行。我知道其中的
try:
from local_settings import *
except ImportError:
pass
伎俩。但我想知道是否有更好的方法来使用 Fabric 从 settings.py
中的 INSTALLED_APPS
中删除我只想在本地运行的应用程序。
I have an app(django-compressor) that I only want to run on my local machine and not the server. I know about the
try:
from local_settings import *
except ImportError:
pass
trick. But I was wondering if there was a better way to remove the app I only want run locally from the INSTALLED_APPS
in the settings.py
using Fabric.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你提到的标准方法是最好的;创建一个包含三个设置文件的文件夹
settings
;shared.py
、development.py
和development.py
。应用的所有实例通用的设置都放置在shared.py
中,并且是从production.py
和development.py
导入的。然后,您可以轻松地在开发设置中添加compressor
shared.py
development.py
您需要确保在开发时运行具有
development.py
设置文件的开发服务器:类似地,在生产服务器上,您对
development.py
执行相同的操作(这取决于您的实现)从长远来看,这是更好的方法,因为您还可以指定单独的缓存、数据库、搜索等设置 也。
顺便说一句,您无需从已安装的应用中完全删除
compressor
,只需使用其COMPRESS_ENABLED
设置来启用和禁用它I think the standard approach you mentioned is best; create a folder
settings
with three settings files;shared.py
,production.py
anddevelopment.py
. Settings that are common to all instances of your app are placed inshared.py
and this is imported fromproduction.py
anddevelopment.py
. Then you can easily only addcompressor
in your development settingsshared.py
development.py
You need to make sure then when developing, you run the development server with the
development.py
settings file:and similarly on your production server you do the same for
production.py
(this is down to your implementation)This is a much better approach in the long term as you can also specify separate cache, database, search etc. settings too.
As an aside, instead of completely removing
compressor
from your installed apps, you can simply enable and disable is using it'sCOMPRESS_ENABLED
setting您也可以用另一种方式来做。
所有共享设置都在settings.py中,并将差异保留在local_settings中。在您的情况下,它是 INSTALLED_APPS,您可以将导入部分更改为如下所示:
这是您的 local_settings.py:
You can also do it in another way.
All the shared settings are in settings.py and keep the difference in local_settings. In your case it is INSTALLED_APPS, you can change your import section to something like this:
And here is your local_settings.py:
我的方法是将原来的
settings.py
(由./manage.py startproject
制作)重命名为base_settings.py
。这样,所有基线设置都在base_settings.py
中。然后,我将创建一个新的
settings.py
文件,该文件仅包含base_settings.py
中所需的特定于环境的修改和覆盖。因此,为了使用我的方法回答您的问题,我的
settings.py
将如下所示:然后,任何必要的特定于环境的设置都将添加到
settings.py
中。通过这种方法,我不需要在调用
./manage.py
时指定--settings
参数或设置DJANGO_SETTINGS_MODULE
。让我更轻松地管理不同的环境。
注意:我使用
git
并将settings.py
添加到.gitignore
以实现此方法。My approach is to rename the original
settings.py
(made by./manage.py startproject
) intobase_settings.py
. This way all the baseline settings are inbase_settings.py
.I will then create a new
settings.py
file that will only contain the needed environment-specific modifications and overrides frombase_settings.py
.So, to answer your question using my approach, my
settings.py
will be like this:Any necessary environment-specific setting will then be added in
settings.py
.With this approach, I don't need to specify the
--settings
parameter when calling./manage.py
or to set theDJANGO_SETTINGS_MODULE
.Makes managing different environments much easier for me.
Note: I use
git
and I addsettings.py
to.gitignore
for this approach.