开发者

debugging django signals' problems

I'm having problems with connecting to a signal in django. I've been following a tutorial available at http://dmitko.ru/?p=546 and tried to extend user registration.

I have django-registration correctly set up. It is working fine. For debug purpose I've put the following code into my urls.py:

from registration.signals import user_registered

def log_user_created(sender, user, request, ** kwargs):
  logger.debug("got USER_REGISTERED signal")

if settings.DEBUG:
  logger.debug("registering debug signal listeners")
  user_registered.connect(log_user_created)
else:
  logger.debug("debuging signals not enabled")    

However the log_user_created function is never called.

My question is: how can I debug my app to see where the user_registered signal is being swallowed?

Note: I've checked that my version of django-registration work开发者_StackOverflow中文版s correctly. I've switched my version with the one from the mentioned blog's example app. It did not change the observed behaviour.


Make sure that the code that connects to the signal and the code that sends signals both import the with the same path. For example, if you connect a signal with:

from myapp.registration.signals import user_registered
user_registered.connect(...)

Then later when you send this signal you must import it with

from myapp.registration.signals import user_registered

(notice the myapp.) and not:

from registration.signals import user_registered

otherwise they'll be treated as two different signals.


Did you try to put user_registered.connect(user_created) in regbackend.py (and not in urls.py) as shown in the example you linked?

You can find some additional info on where to best connect your signals in this question.

To see if user_registered is called correctly you simply could add a debug call at the beginning of the user_created function.

django-debug-toolbar, though i haven't used that specific feature, might be another option:

Currently, the following panels have been written and are working:

  • List of signals, their args and receivers


I created a sample application, that you can play with (I believe it works) http://dmitko.ru/samples/sample_user_registration.zip

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜