Reputation: 705
i want this functionality. User enters email address, and somehow it has to be passed to my views.py file, so i could then email the user that he has succesfully registered.
This is my template file:
{% extends "base.html" %}
{% block content %}
<section>
<h2 style="text-align: center">Register</h2>
<form action="/accounts/register/" method="post">{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type="submit" value="Register" onclick="validateForm()"/>
</form>
</section>
{% endblock %}
this is my forms.py file:
class MyRegistrationForm(UserCreationForm):
#kokie fields bus displayed html form
email = forms.EmailField(required=True)
firstname = forms.CharField(required=True)
lastname = forms.CharField(required=True)
whoinvitedyou = forms.CharField(required=True)
phone = forms.CharField(required=True)
workplace = forms.CharField(required=True)
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2', 'firstname', 'lastname', 'whoinvitedyou', 'phone', 'workplace')
def save(self, commit=True):
user = super(MyRegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.set_password(self.cleaned_data["password1"])
#more fields for name last name
user.firstname = self.cleaned_data['firstname']
user.lastname = self.cleaned_data['lastname']
user.whoinvitedyou = self.cleaned_data['whoinvitedyou']
user.phone = self.cleaned_data['phone']
user.workplace = self.cleaned_data['workplace']
if commit:
user.save()
return user
this is my views.py:
def register_user(request):
if request.method == 'POST':
form = MyRegistrationForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/accounts/register_success')
else:
return render_to_response('invalid_reg.html')
args = {}
args.update(csrf(request))
args['form'] = MyRegistrationForm()
print args
return render_to_response('register.html', args)
how do i pass the value so later i can use it? maybe somebody can help me with this...
Upvotes: 0
Views: 555
Reputation: 1672
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.', '[email protected]', [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
Upvotes: 0
Reputation: 668
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.
Upvotes: 1