每次我通过SSH登录我的服务器时,我都需要输入以下内容:
export DJANGO_SETTINGS_MODULE=settings
Run Code Online (Sandbox Code Playgroud)
如果我没有使用manage.py模块失败
我的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"
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
jat*_*ism 11
您manage.py正在引用应用程序(notifications).这迫使Django抱怨DJANGO_SETTINGS_MODULE被设置,因为还没有设置Django环境.
顺便说一下,您可以手动强制进行环境设置,但老实说,我不会在manage.py中执行此操作.在我看来,这不是一个好习惯.
以下是如何从任何应用程序(或程序)中手动设置Django环境:
# set up the environment using the settings module
from django.core.management import setup_environ
from myapp import settings
setup_environ(settings)
Run Code Online (Sandbox Code Playgroud)
您需要设置DJANGO_SETTINGS_MODULE环境变量,因为它是Django如何知道您的设置模块被调用的方式(因此您可以在每个项目中使用不同的模块或进行测试和开发.)您可以在导入django 之前在脚本中自行设置它(直接或间接地)但是当你运行Django提供的脚本时,这不会有太大作用.
最简单的解决方案可能是在shell的启动脚本中设置DJANGO_SETTINGS_MODULE,因此您不必再手动设置它.添加它的常用文件是.bash_profile和.bashrc(如果确实使用了bash.)