为什么信号不触发? [英] Why does the signal not trigger?

查看:291
本文介绍了为什么信号不触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

坐在一天之内。真的不明白为什么这个信号在用户被激活时没有被触发,没有错误日志,管理员在激活时也不例外。有人可以帮忙吗?以下代码应该会导致apache error.log中的日志消息在用户的时候呢?

  import logging 
from django.dispatch import receiver
from registration.signals import user_activated

@receiver(user_activated)
def registered_callback(sender,** kwargs):
logger = log.getLogger(user-activated)
logger.error(activate here)

user_registered

解决方案

首先使用 django 1.8.3 。你应该首先注册你的信号。据我所知,有一些方法可以做到这一点,但这正是我做的事情。
在你的应用程序中创建 signals.py 在那里写信号;

  from django.dispatch import receiver 
@receiver(post_save,sender = your_model,dispatch_uid =yourmodel_save_receiver)
def post_save_yourmodel(sender ,例如** kwargs)
如果instance.profile_status:
打印活动
其他:
打印不活动

然后你应该创建 apps.py 。此文件包含您的模型的配置信息。

 从django.apps导入AppConfig 
class yourmodel_config(AppConfig):
name ='yourmodel_config'
verbose_name ='your_model config'

def ready(self):
import yourmodel.signals

随着应用程序准备就绪,您的信号将被导入
最后打开您的 __ init __。py 并添加以下内容。

  default_app_config ='yourmodel.apps.yourmodel_config'

这样你就可以为你的模型定义应用程序配置了。这个例子当有 yourmodel 被保存,信号检查 profile_status 属性,并根据值(true或false)打印输出到您的控制台。您还可以将创建的参数添加到模型中,以了解如果创建了模型的实例。如果创建新记录,创建将返回True。 def post_save_yourmodel(发件人,实例,创建,** kwargs):。否则,当您的模型使用 yourmodel.save()保存时,此信号将被触发。
考虑这是一个 post_save 示例。您可以从 here


Sitting over a day on it. Really can't understand why this signal is not triggered when a user is activated, no error log, no exception in the admin on activation. Can anybody help? The following code should result in a log message in the apache error.log when a user, right?

import logging
from django.dispatch import receiver
from registration.signals import user_activated

@receiver(user_activated)
def registered_callback(sender, **kwargs):
    logger = logging.getLogger("user-activated")
    logger.error("activated here")

same with user_registered

解决方案

First of all im using django 1.8.3 .You should register your signal first. As far as i know, there are some methods to do that but this is what im doing; Create signals.py in your app write your signal there;

from django.db.models.signals import post_save
from django.dispatch import receiver
@receiver(post_save, sender=your_model,dispatch_uid="yourmodel_save_receiver")
            def post_save_yourmodel(sender, instance, **kwargs):
                 if instance.profile_status:
                     print "active"
                 else:
                     print "not active"

Then you should create apps.py. This file contains configuration information to your model.

from django.apps import AppConfig
class yourmodel_config(AppConfig):
    name = 'yourmodel_config'
    verbose_name = 'your_model config'

    def ready(self):
        import yourmodel.signals

With this whenever your app is ready, your signals will be imported Finally open your __init__.py and add the following.

default_app_config = 'yourmodel.apps.yourmodel_config'

With this you are defining application configuration for your model.This example when ever yourmodel is saved, signal checks for profile_status attribute and prints output depending on the value(true or false) to your console. You can also add created parameter to your model to know that if instance of the model is created. created will return True if a new record was created. def post_save_yourmodel(sender, instance, created, **kwargs):. Otherwise this signal will be triggered whenever your model is saved with yourmodel.save(). Consider that is a post_save example.You can find list of the model signals from here.

这篇关于为什么信号不触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆