[Django]-How can I send an email to user on new user addition to django admin site?

1👍

You want to hook the post_save signal for the User model.

3👍

There are multiple problems I can see in the code as I can see it, above.

  • First, the def email_new_user() is wrongly indented. If that is not a formatting error here, correct it.
  • new_user = kwargs.["instance"] is wrong syntax. It really should be kwargs["instance"]

And then, do you have an SMTP server running? Can you send email from the shell? If not, configure that and then try it again. It should work.

👤lprsd

0👍

One possible problem was, Django user cretion consists of two steps. First django asks for username, password and password confirmation, and when you press save (or save and continue edit) your save method fired without e-mail information. Since django will accept that request as a newly created record, your signal will fire for a record with no e-mail information…

If django handles admin save diffrently and do not mark a newly created record instance as created you shall not have any problem, but probably django will not do that.

UPDATE: Some possible reasons are:

  • You need django 1.3, so check your django version with python manage.py version and update it if required…
  • Best place for your your models.py files

You can put signal handling and registration code anywhere you like. However, you’ll need to make sure that the module it’s in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app’s models.py a good place to put registration of signal handlers.

These are basic problems…

👤Mp0int

Leave a comment