Connecting a django-registration signal

巧了我就是萌 提交于 2019-12-06 08:06:32

问题


I have a function:

def create(sender, **kw):
  [...]

Which should be called when the user_activated signal from django-registration is called.

I connect the signal and the function using this:

from registration.signals import user_activated
[...]
post_save.connect(create, sender=user_activated, dispatch_uid="users-atactivation-signal")

But the function isn't called when a user clicks on the activation link, which he got by email.

What do I miss here.


回答1:


A function like this:

def create(sender, user, request, **kwarg):
[...]

and a connect call like this:

user_activated.connect(create)

does the job. I have these in my signals.py file.




回答2:


If the django-registration app wasn't actually installed, but rather just copied into a project your code that listens for signals won't get called properly.

From the django-registration v0.8 documentation:

I’ve got functions listening for the registration/activation signals, but they’re not getting called!

The most common cause of this is placing django-registration in a sub-directory that’s on your Python import path, rather than installing it directly onto the import path as normal. Importing from django-registration in that case can cause various issues, including incorrectly connecting signal handlers. For example, if you were to place django-registration inside a directory named django_apps, and refer to it in that manner, you would end up with a situation where your code does this:

from django_apps.registration.signals import user_registered

But django-registration will be doing:

from registration.signals import user_registered

From Python’s point of view, these import statements refer to two different objects in two different modules, and so signal handlers connected to the signal from the first import will not be called when the signal is sent using the second import.

To avoid this problem, follow the standard practice of installing django-registration directly on your import path and always referring to it by its own module name: registration (and in general, it is always a good idea to follow normal Python practices for installing and using Django applications).




回答3:


user_activated is itself is a Signal. So you have to send itself, with parameters. It requires 2 arguments apart from sender, i.e. user, request

user_activated.send(sender=Foo, user=request.user, request=request)

Foo is the backend class used to activate the user.




回答4:


Is the code that connects the signal-handling method to that signal definitely being loaded? (You can test with a print statment immediately after it). You can make sure you load your signal handlers for a given app by importing them from that app's __init__.py:

from nameofapp.nameoffilewithhandlersinit import *

PS. is that a typo in the dispatch_uid, or a deliberate name?



来源:https://stackoverflow.com/questions/3430937/connecting-a-django-registration-signal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!