python 为什么我需要 DJANGO_SETTINGS_MODULE 集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2102330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:47:05  来源:igfitidea点击:

Why do I need the DJANGO_SETTINGS_MODULE set?

pythondjango

提问by RadiantHex

Every time I log on to my server through SSH I need to type the following:

每次我通过 SSH 登录到我的服务器时,我都需要输入以下内容:

export DJANGO_SETTINGS_MODULE=settings

if I do not any usage of the manage.py module fails

如果我没有使用 manage.py 模块失败

My manage.py has the following added code:

我的 manage.py 添加了以下代码:

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

    def create_notice_types(app, created_models, verbosity, **kwargs):
        notification.create_notice_type("friends_invite", _("Invitation Received"), _("you have received an invitation"))
        notification.create_notice_type("friends_accept", _("Acceptance Received"), _("an invitation you sent has been accepted"))

    signals.post_syncdb.connect(create_notice_types, sender=notification)
else:
    print "Skipping creation of NoticeTypes as notification app not found"

Any ideas?

有任何想法吗?

采纳答案by jathanism

Yourmanage.pyis referencing an application (notifications). This forces Django to complain about DJANGO_SETTINGS_MODULE being set because the Django environment hasn't been set up yet.

manage.py正在引用一个应用程序 ( notifications)。这迫使 Django 抱怨 DJANGO_SETTINGS_MODULE 被设置,因为尚未设置 Django 环境。

Incidentally, you can force the enviroment setup manually, but honestly I wouldn't do this in manage.py. That's not really a good practice in my opinion.

顺便说一句,您可以手动强制设置环境,但老实说我不会在 manage.py 中这样做。在我看来,这并不是一个好的做法。

Here is how you can manually setup the Django environment from within any app (or program for that matter):

以下是从任何应用程序(或程序)中手动设置 Django 环境的方法:

# set up the environment using the settings module
from django.core.management import setup_environ
from myapp import settings
setup_environ(settings)

回答by Thomas Wouters

You need to set the DJANGO_SETTINGS_MODULE environment variable because it's how Django knows what your settings module is called (so you can have different ones per project or for testing and development.) You can set it in the scripts themselves beforeyou import django (directly or indirectly) but that won't do much good when you run the Django-provided scripts.

您需要设置 DJANGO_SETTINGS_MODULE 环境变量,因为它是 Django 知道您的设置模块被调用的方式(因此您可以为每个项目或测试和开发使用不同的模块。)您可以在导入 django之前在脚本中设置它(直接或间接)但是当您运行 Django 提供的脚本时,这不会有多大好处。

The easiest solution is probably to just set DJANGO_SETTINGS_MODULE in your shell's startup scripts, so you won't have to set it manually anymore. The usual files to add it to are .bash_profile and .bashrc (if you do indeed use bash.)

最简单的解决方案可能是在 shell 的启动脚本中设置 DJANGO_SETTINGS_MODULE,这样您就不必再手动设置它了。添加它的常用文件是 .bash_profile 和 .bashrc (如果你确实使用 bash。)

回答by jcdyer

By default, manage.py looks for a settings module in the same directory as itself. If it doesn't find one, it bombs out with a message to use django-admin.py instead. It doesn't actually set up the environemnt until it runs execute_manager. If you need to run your hooks before calling your management functions, the practice I've seen suggested is to put them in the relevant app's models.py.

默认情况下, manage.py 在与其自身相同的目录中查找设置模块。如果它没有找到,它会用一条消息来代替使用 django-admin.py。在它运行之前,它实际上并没有设置环境execute_manager。如果您需要在调用管理函数之前运行钩子,我建议的做法是将它们放在相关应用程序的models.py.