[Django]-Connecting a django-registration signal

5👍

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.

👤Kai

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

👤Raj

0👍

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.

0👍

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?

Leave a comment