简体   繁体   English

django:连接到现有信号失败

[英]django: connecting to existing signal fails

I started using django_openid_auth ( https://launchpad.net/django-openid-auth ) and it works just fine. 我开始使用django_openid_auth( https://launchpad.net/django-openid-auth ),它可以正常工作。

I manage to connect to my openid provider and get user authenticated. 我设法连接到我的openid提供程序并获得用户身份验证。 Thing is that in django_open_auth.views.login_complete there is this block: 问题是,在django_open_auth.views.login_complete中有以下代码块:

if user is not None:
    if user.is_active:
        auth_login(request, user)
        response = HttpResponseRedirect(sanitise_redirect_url(redirect_to))

        # Notify any listeners that we successfully logged in.
        openid_login_complete.send(sender=UserOpenID, request=request,
            openid_response=openid_response)

        return response
    else:
        return render_failure(request, 'Disabled account')

Notice the #notify comment. 注意#notify注释。

I created my own OpenIdBackend subclass of django-openid-auth OpenIDBackend class and put it into core.authentication.openidbackend of my own project 我创建了django-openid-auth OpenIDBackend类的自己的OpenIdBackend子类,并将其放入我自己项目的core.authentication.openidbackend中

In that file there is: from django_openid_auth.signals import openid_login_complete 在该文件中有:从django_openid_auth.signals导入openid_login_complete

def update_request(sender, **kwargs):
    logger.debug('test')

openid_login_complete.connect(update_request)

But that debug line never shows up in log. 但是该调试行永远不会显示在日志中。

So what am i doing wrong? 那我在做什么错? I checked some other django signals threads and one of them said that the import must be exactly the same, otherwise the connection does not appear. 我检查了其他django信号线程,其中一个说导入必须完全相同,否则连接不会出现。

But how can i even debug this? 但是我怎么能调试呢? Where is the problem - does the connection never occur cause i put the connect into wrong place or is the problem elsewhere? 问题出在哪里-连接永远不会发生是因为我将连接放到错误的位置还是其他地方的问题? How can i find this out? 我如何找到这个?

Alan 艾伦

Edit: I tried everything that was suggested at once and i got it working. 编辑:我尝试了所有建议一次,并且我能正常工作。 I put the signal into separate file, imported it in correct place and also followed the advice given by karthikr ( https://stackoverflow.com/users/1628832/karthikr ). 我将信号放入单独的文件中,并将其导入正确的位置,并遵循karthikr( https://stackoverflow.com/users/1628832/karthikr )给出的建议。 I also noticed that my logger was not using correct logging conf so everything may have been working earlier, just that logging did not work. 我还注意到,我的记录器未使用正确的日志记录conf,因此所有内容可能早就可以正常工作了,只是日志记录不起作用。

So yeah :). 是的 :)。 Should not work when you are not feeling well :). 当您感觉不适时不应该工作:)。 Thanks for the help guys. 感谢您的帮助。

Where are you coding that signal callback? 您在哪里编码该信号回调? Are you importing the module where you register the callback before the signal gets sent? 您是否在发送信号之前将模块导入注册回调的位置?

For example, I have used signals and code the in my_app/signals.py . 例如,我已经使用信号并在my_app/signals.py编码。 Later, I go to my_app/models.py and add the line: 稍后,我转到my_app/models.py并添加以下行:

import my_app.signals

The file where you write your callbacks must be imported before the signals raises : 在产生信号之前,必须先导入编写回调的文件:

Where should this code live? 该代码应该放在哪里? You can put signal handling and registration code anywhere you like. 您可以在任何喜欢的地方放置信号处理和注册代码。 However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent. 但是,您需要确保所导入的模块尽早导入,以便在需要发送任何信号之前注册信号处理。 This makes your app's models.py a good place to put registration of signal handlers. 这使您的应用程序的models.py成为注册信号处理程序的好地方。

Also you can try to register with the @register decorator. 您也可以尝试使用@register装饰器进行注册。 It is pretty much the same but you can try to see if it works the other way: 几乎是相同的,但是您可以尝试查看它是否以另一种方式起作用:

from django_openid_auth.signals import openid_login_complete
from django.dispatch import receiver

@receiver(openid_login_complete)
def update_request(sender, **kwargs):
    logger.debug('test')

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM