最近、django-notification-1 をインストールしました。必要に応じてコードを参照してください。DEBUG=True または DEBUG=False を指定してローカル開発サーバーでアプリを実行すると、すべて正常に動作します。DEBUG=True の heroku ではすべて問題ありませんが、DEBUG=False の場合、モジュール「通知」が存在しないというインポート エラーが発生します。
これは、インポート エラーが発生するコア アプリ内の models.py ファイルです。このファイルを使用する唯一の目的は、他のすべてのアプリからの信号と通知を処理することです。最初の定義の後にファイルを切り捨てました:
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 runserver で機能します。
問題は、開発サーバーではなく、heroku の DEBUG の状態に基づいてインポートの動作が変化する原因は何ですか? さらに重要なのは、なぜ DEBUG がこれに影響を与えるのでしょうか?