django-registration 0.8 signal not working
-
25-09-2019 - |
Question
Hey there, Now im working this base on dmitko tutorial on extending django-registration post, all went out fine, just the i can't received the user_registered signal properly.
forms.py
from django import forms
from registration.forms import RegistrationForm
from models import UserProfile
class UserProfileForm(RegistrationForm):
fullname = forms.CharField(max_length=200)
address = forms.CharField(max_length=200)
urls.py
from django.conf.urls.defaults import *
from registration.views import register
from forms import UserProfileForm
import regbackend
urlpatterns = patterns('',
url(r'^accounts/register/$', register, {'backend': 'registration.backends.default.DefaultBackend','form_class': UserProfileForm}, name='registration_register'),
(r'^accounts/', include('registration.backends.default.urls')),
)
regbackend.py
from forms import UserProfileForm
from models import UserProfile
def user_created(sender, user, request, **kwargs):
form = UserProfileForm(request.POST)
data = UserProfile(user=user)
data.fullname = form.cleaned_data["fullname"]
data.address = form.cleaned_data["address"]
print "USER CREATED SIGNALS!"
data.save()
from registration.signals import user_registered
user_registered.connect(user_created)
print "REGBACKEND!"
I can get all them working fine but I can't get my signal to connect properly to my user_created method. The print method are just a way for me to check the codes. Can't get user_created to print "USER CREATED SIGNALS!".
Any thoughts??
Solution
Signal's listener must be initiated before calling the signal - try to import ragbackend.py in models.py before registering a signal - if it will start working you have the clue about solution.
Also do not use print, use python built-in logging module instead. You could also run debugging session with ie. ipdb to see if the signal is received and if the user is created, ot you could write simple unit-test or doctest to verify that.