Activating user account with an email
Django registration with confirmation email
When we signup on website its send a email for confirmation to active an account. Or sometime 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 deploy this. But there other option also to deploy 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.
There is my project directory
|
|
Configure settings
Firstly we configure email host server in in
settings.py
for sending confirmation email.mysite/settings.py
|
|
Added this lines in your
settings.py
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
Create Tokens
Now we have to create token that we will send for confirmation.
Create a new files named
tokens.py
in blog appblog/tokens.py
|
|
Basically it extends PasswordResetTokenGenerator to create token.
Django forms
I think best simple way to implement a user registration page on Django is using
UserCreationForm
. Here is my forms.py
file.blog/forms.py
|
|
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.blog/views.py
|
|
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.blog/templates/acc_active_email.html
:
|
|
This template create a email body with activate link that will send for application.
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.blog/views.py
|
|
Added this
activate
function after signup
function in blog/views.py
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
.Urls
blog/urls
|
|
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.
blog/templates/signup.html
|
|
This template will shown like this
Comments
Post a Comment