SuccessView (Displays success template).CheckEmailView (Points users to check their email).ActivateView (Activates the user’s account).These are the views we’re going to build: We’re going to create multiple views, so if at any point you need a guide, check out the source code of the project. We created this method inside the form because views should have the least amount of logic possible. We use the request to get the current site domain (localhost:8000 in our case), and the user to get its base64 encoded ID and a one-time token.Īll of this information is passed as context to a template users/activate_account.html, which we’ll create later.įinally, we send an email to the user with the respective subject, message, and html message. This method accepts two arguments: the request, and a User object. This is a rather huge form, but let’s focus on the send_activation_email(). User.email_user(subject, message, html_message=message) 'token': token_generator.make_token(user), 'uid': urlsafe_base64_encode(force_bytes(user.pk)), # We need the user object, so it's an additional parameterĭef send_activation_email(self, request, user): Max_length=254, help_text='Enter a valid email address') If you’re following along with a Unix based OS like Linux or macOS, open a terminal and run the following commands to create and source a virtual environment named venv:įrom import UserCreationFormįrom import get_current_siteįrom import get_user_modelįrom import force_bytesįrom import urlsafe_base64_encodeįrom import render_to_string This allows you to manage the dependencies of each project you have. It’s considered a good practice to set up a Python virtual environment for every Django project you start. To complete this tutorial, you’ll need the following: Whenever you want a quick reference to the code, you can visit this GitHub repository. ![]() We’ll create a complete project from scratch but if you already have one, don’t hesitate in following along with this tutorial. This tutorial will teach you how to build confirmation emails to verify users’ registration using Django and SendGrid.ĭjango has a built-in mail sending interface, which we’ll be using to send emails with SendGrid through the tutorial.Īfter we finish, you’ll have an authentication system that requires users to verify their account with a confirmation email. ![]() If you don’t, you’re decreasing the possibility of contacting your users and opening a door for spammers to create fake accounts and affect your website. Any web application that includes authentication processes should have some sort of email activation flow.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |