使用 Fabric 的 INSTALLED_APPS 本地设置

发布于 2025-01-08 07:37:59 字数 272 浏览 0 评论 0原文

我有一个应用程序(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾挽 2025-01-15 07:37:59

我认为你提到的标准方法是最好的;创建一个包含三个设置文件的文件夹 settingsshared.pydevelopment.pydevelopment.py。应用的所有实例通用的设置都放置在 shared.py 中,并且是从 production.pydevelopment.py 导入的。然后,您可以轻松地在开发设置中添加 compressor

shared.py

INSTALLED_APPS = (...)

development.py

from settings.shared import *
INSTALLED_APPS += ('compressor',)

您需要确保在开发时运行具有 development.py 设置文件的开发服务器:

python manage.py --settings=settings.development 

类似地,在生产服务器上,您对 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 and development.py. Settings that are common to all instances of your app are placed in shared.py and this is imported from production.py and development.py. Then you can easily only add compressor in your development settings

shared.py

INSTALLED_APPS = (...)

development.py

from settings.shared import *
INSTALLED_APPS += ('compressor',)

You need to make sure then when developing, you run the development server with the development.py settings file:

python manage.py --settings=settings.development 

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's COMPRESS_ENABLED setting

浪荡不羁 2025-01-15 07:37:59

您也可以用另一种方式来做。

所有共享设置都在settings.py中,并将差异保留在local_settings中。在您的情况下,它是 INSTALLED_APPS,您可以将导入部分更改为如下所示:

DEV_APPS = None
try:
    from local_settings import *
    INSTALLED_APPS += DEV_APPS
except:
    PASS

这是您的 local_settings.py:

DEV_APPS = ('compressor',)

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:

DEV_APPS = None
try:
    from local_settings import *
    INSTALLED_APPS += DEV_APPS
except:
    PASS

And here is your local_settings.py:

DEV_APPS = ('compressor',)
勿忘初心 2025-01-15 07:37:59

我的方法是将原来的settings.py(由./manage.py startproject制作)重命名为base_settings.py。这样,所有基线设置都在 base_settings.py 中。

然后,我将创建一个新的 settings.py 文件,该文件仅包含 base_settings.py 中所需的特定于环境的修改和覆盖。

因此,为了使用我的方法回答您的问题,我的 settings.py 将如下所示:

from .base_settings import *

INSTALLED_APPS += ('compressor',)

然后,任何必要的特定于环境的设置都将添加到 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) into base_settings.py. This way all the baseline settings are in base_settings.py.

I will then create a new settings.py file that will only contain the needed environment-specific modifications and overrides from base_settings.py.

So, to answer your question using my approach, my settings.py will be like this:

from .base_settings import *

INSTALLED_APPS += ('compressor',)

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 the DJANGO_SETTINGS_MODULE.

Makes managing different environments much easier for me.

Note: I use git and I add settings.py to .gitignore for this approach.

执笔绘流年 2025-01-15 07:37:59

以添加调试工具为例。
Django==2.1.7

# proj/settings/defaults.py
....

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    # apps
    'apps.document.apps.DocumentConfig',
]
# proj/settings/dev.py

DEBUG = True
from .defaults import *

INSTALLED_APPS += ['debug_toolbar']
# proj/urls.py
if settings.DEBUG:
    import debug_toolbar

    urlpatterns = [
                      path('__debug__/', include(debug_toolbar.urls)),
                  ] + urlpatterns

./manage.py runserver --settings=proj.settings.dev

Take add debug tools for example .
Django==2.1.7

# proj/settings/defaults.py
....

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    # apps
    'apps.document.apps.DocumentConfig',
]
# proj/settings/dev.py

DEBUG = True
from .defaults import *

INSTALLED_APPS += ['debug_toolbar']
# proj/urls.py
if settings.DEBUG:
    import debug_toolbar

    urlpatterns = [
                      path('__debug__/', include(debug_toolbar.urls)),
                  ] + urlpatterns

./manage.py runserver --settings=proj.settings.dev
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文