Django registration with confirmation email


When we sign up on website its send a email for confirmation to active an account. Or confirmation email need to change password or change email of an account.

Here i will show you how to send a confirmation email when someone register on your web app that deploy on Django.

I will discuss about the normal way to build this. But there other option also to build this. Like django-registration, django-registration-redux, django-allauth application. Those application made this very easy, also integrated with authentication, account management, social account authentication etc.

Lets start

I have a project called mysite and an app called blog

This is my project directory



Configure settings

Firstly we configure email host server in for sending confirmation email.


EMAIL_HOST_PASSWORD = 'yourpassword'

Added this lines in your file.

Here, I used gmail smtp host server, you can use others smtp server also. If you get error then follow this answer, its says that allow less secure apps and display unlock captcha

Token Generator

We have to create token that will use in email confirmation url.


from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class TokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type( + six.text_type(timestamp) +
account_activation_token = TokenGenerator()

using this class we can create a unique token for confirmation. I have use django PasswordResetTokenGenerator class that used for password reset.

Django FORMS

I think best simple way to implement a user registration page on Django is using UserCreationForm. Here is my file.


from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class SignupForm(UserCreationForm):
email = forms.EmailField(max_length=200, help_text='Required')
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')

I have import UserCreationForm forms and User models. Then added an extra field email in SignupForm. This email field take email address while registering for email confirmation.


from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from .forms import SignupForm
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.template.loader import render_to_string
from .tokens import account_activation_token
from django.contrib.auth.models import User
from django.core.mail import EmailMessage
def signup(request):
if request.method == 'POST':
form = SignupForm(request.POST)
if form.is_valid():
user =
user.is_active = False
current_site = get_current_site(request)
mail_subject = 'Activate your blog account.'
message = render_to_string('acc_active_email.html', {
'user': user,
'domain': current_site.domain,
to_email = form.cleaned_data.get('email')
email = EmailMessage(
mail_subject, message, to=[to_email]
return HttpResponse('Please confirm your email address to complete the registration')
form = SignupForm()
return render(request, 'signup.html', {'form': form})

Update: For newer version of django add decode() when generate uid
To know more see this

Here it got the form information using POST method, then valid it. Notice that i have write user.is_active = False so that user can’t login without email confirmation.

Then write email subject, message and send it by EmailMessage() function. Email message create by a template.


{% autoescape off %}
Hi {{ user.username }},
Please click on the link to confirm your registration,
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}

This template create a email body with activate link that will send for application.

Create Activate function

User will get an activate link to their email address. Now we have to active their account through activation link.

By clicking on activation link the user send to the activate view.


def activate(request, uidb64, token):
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
login(request, user)
# return redirect('home')
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
return HttpResponse('Activation link is invalid!')

Added this activate function after signup function in blog/ file. This function will check token if it valid then user will active and login. Notice that i have write user.is_active=True. Before confirming email this was False.



from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name='home'),
url(r'^signup/$', views.signup, name='signup'),
views.activate, name='activate'),

This is the urls for this app. Last url for email activate confirmation.

Sign up Template

Finally we have create the signup template, that user use for registration.


{% block content %}
<h2>Sign up</h2>
<form method="post">
{% csrf_token %}
{% for field in form %}
{{ field.label_tag }}<br>
{{ field }}
{% if field.help_text %}
<small style="display: none">{{ field.help_text }}</small>
{% endif %}
{% for error in field.errors %}
<p style="color: red">{{ error }}</p>
{% endfor %}
{% endfor %}
<button type="submit">Sign up</button>
{% endblock %}

This template will shown like this

template view

This post is previously published on my blog.

Project code




Code — Sleep — Travel

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Even more on SQL Subqueries

Time in Elm

Neovim for Beginners —Plugin Management

Not every security software is created equally

Python Functions with Outputs!

Exceptions Handling in Java

Deno Websocket Chat With OAK Middleware

Taking .NET Core into Production

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Farhadur Reja Fahim

Farhadur Reja Fahim

Code — Sleep — Travel

More from Medium

Django Plausible Proxy

CRUD with QuerySet API in Python and Django


Set up and Run Django First Application