【问题标题】:Setting up a basic signal receiver in the sandbox app在沙盒应用程序中设置基本信号接收器
【发布时间】:2020-11-16 13:04:32
【问题描述】:

我正在尝试在沙盒应用中设置信号接收器。我使用以下代码创建了/sandbox/signals/receivers.py

import logging

from django.dispatch import receiver
from oscar.apps.catalogue.signals import product_viewed


@receiver(product_viewed)
def receive_product_view(sender, product, user, **kwargs):
    logging.info("receive_product_view")
    if user and user.is_authenticated:
        logging.info("Product {product} viewed by user {user}")
    else:
        logging.info("Product {product} viewed anonymously")

这似乎没有任何作用。在/sandbox/__init__.py 我做import signals 和我有/sandbox/signals/__init__.pyimport receivers 所以我假设它在启动时加载该模块(这可能是一个错误的假设)。

当我在浏览器中加载产品页面时,我在控制台日志中看不到任何内容。是不是我的日志记录设置错误,或者这是连接信号接收器的错误方式?

【问题讨论】:

    标签: django-oscar


    【解决方案1】:

    Django 文档建议您从 ready() method of your application configuration class 内部导入信号接收器。 Here is an example奥斯卡本身是如何做到这一点的。

    我怀疑将接收器导入您当前正在执行的操作根本不起作用 - sandbox 本身并没有作为模块加载,它只是一个包含其他模块的目录。

    Oscar 附带的沙盒没有任何您可以添加的应用程序 - 因此您需要将其添加到 apps 目录,然后将其包含在 INSTALLED_APPS 设置中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 2017-08-15
      • 2014-04-13
      • 2015-09-12
      相关资源
      最近更新 更多