My registration page accepts username, email and password. I want to include email address verification to it. A link that will be send to the user's email id and then after they verify,the registration process completes.
signup.html
{% extends 'fir/base_visitor.html' %}
{% block title %}Sign Up{% endblock %}
{% block signup_active %}active{% endblock %}
{% block body %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<h3>Sign up for an Account</h3>
{% if error_message %}
<p><strong>{{ error_message }}</strong></p>
{% endif %}
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'fir/form_template.html' %}
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
</div>
<div class="panel-footer">
Already have an account? <a href="{% url 'fir:signin_user' %}">Click here</a> to sign in.
</div>
</div>
</div>
</div>
</div>
{% endblock %}
views.py
def signup(request):
form = UserForm(request.POST or None)
if form.is_valid():
user = form.save(commit=False)
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user.set_password(password)
user.save()
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
evidences = Evidence.objects.filter(user=request.user)
return render(request, 'fir/index.html', {'evidences': evidences})
context = {
"form": form,
}
return render(request, 'fir/signup.html', context)
This is for activation and I also have used tokens.
def activate(request, uidb64, token):
try:
uid = force_text(urlsafe_base64_decode(uidb64))
user = User.objects.get(pk=uid)
except(TypeError, ValueError, OverflowError, User.DoesNotExist):
user = None
if user is not None and account_activation_token.check_token(user, token):
user.is_active = True
user.save()
login(request, user)
return HttpResponse('Thank you for your email confirmation. Now you can login to your account.')
else:
return HttpResponse('Activation link is invalid!')
I have also included acc_active_email.html and acc_active_sent.html to my templates. Now,in my registration part in views.py, how do I edit my code to add these lines of code?
current_site = get_current_site(request)
message = render_to_string('acc_active_email.html', {
'user':user, 'domain':current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
# Sending activation link in terminal
# user.email_user(subject, message)
mail_subject = 'Activate your blog account.'
to_email = form.cleaned_data.get('email')
email = EmailMessage(mail_subject, message, to=[to_email])
email.send()
return HttpResponse('Please confirm your email address to complete the registration.')
And,I have also included the email tls thing in settings.py.Please do help me with it. Thank you.And, everything is properly added to urls.py as well.