Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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).

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.

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top