1

我最近安装了 django-notification-1,如有必要,请参阅此处的代码。当我使用 DEBUG=True 或 DEBUG=False 在本地开发服务器上运行我的应用程序时,一切正常。在带有 DEBUG=True 的 heroku 上,一切都很好,但是当 DEBUG=False 时,我收到一个导入错误,模块“通知”不存在。

这是我的核心应用程序中发生导入错误的 models.py 文件。我使用此文件的唯一目的是处理来自所有其他应用程序的信号和通知。我在第一个 def 之后截断了文件:

from django.contrib.auth.models import User
from django.contrib.comments.models import Comment
from django.db.models.signals import post_save
from follow.signals import followed
from django.contrib.comments.signals import comment_was_posted, comment_was_flagged

from django.contrib.sites.models import Site
from django.conf import settings
from django.dispatch import receiver
from django.template import Context

from notification import models as notification


@receiver(followed, sender=User, dispatch_uid='follow.user')
def user_follow_handler(user, target, instance, **kwargs):

    if user != target:
        notification.send([target], "followed", {"from_user": user}, sender=user)

导入在以下行失败:

from notification import models as notification

我通过将导入放在函数中来临时修复它,然后它甚至可以在 DEBUG=False 的情况下工作:

@receiver(followed, sender=User, dispatch_uid='follow.user')
def user_follow_handler(user, target, instance, **kwargs):
    from notification import models as notification 
    if user != target:
        notification.send([target], "followed", {"from_user": user}, sender=user)

显然,在加载通知应用程序之前调用了导入。但是为什么只有在使用 DEBUG=False 的 heroku 上才有问题。如果我在 heroku 上设置 DEBUG=True 一切正常,无论哪种方式它都适用于 models.py 运行服务器。

问题是:什么会导致导入行为根据 heroku 上的调试状态而不是我的开发服务器上的状态发生变化?更重要的是,为什么 DEBUG 会对此产生影响?

4

0 回答 0