[Answer]-Django how to pass value from template to view

1👍

In your view, after the form.is_valid() call, the email address will be available in form.cleaned_data['email']. You can use that to send the email after form.save().

Additionally, you might want to look into existing 3rd party libraries like django-registration as it already does the functionality (emailing the just registered user) that you want.

0👍

In order to send an e-mail you don’t necessarily need to send the value to a view in views.py.
You can use a post_save signal to send an email. You can put the code anywhere, although I usually put it in models.py.

Info on signals: https://docs.djangoproject.com/en/1.7/topics/signals/

from django.db.models.signals import post_save
from django.dispatch import receiver
from django.core.mail import send_mail

@receiver(post_save, sender=User)
def my_handler(sender, instance, *args, **kwargs):
    send_mail('Subject of email', 'Message body.', 'from@example.com', [instance.email], fail_silently=False)

note that instance.email is the email address of the user which you just saved, you can access instance to retreive more information e.g. the name, so that you can put "dear "+instance.name at the beginning of the body for example

Leave a comment