Shubham Dubey
Shubham Dubey

Reputation: 1

How to Verify Email in Django using OTP and UserCreationForm

I am creating a Quiz App. In that I have a sign up page for user authentication which is using the default User Model and User CreationForm. I want to add a email verification in it by sending a otp on the email.

Please explain me how can I send otp on email using default user model and user creation form to verify the otp on the email.

My user app code is:

forms.py

from django import forms from django.contrib.auth import get_user_model from django.contrib.auth.forms import UserCreationForm

    email = forms.EmailField()

    class Meta:
        model = get_user_model()
        fields = ['username', 'email', 'password1', 'password2']

views.py

import pyotp
from django.contrib.auth.models import User
from .forms import UserRegisterForm
from django.contrib import messages
from django.shortcuts import render, redirect

def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            otp=pyotp.totp('base32secret3232')
            form.save()
            messages.success(request, f'Account Created for {User.username}. You can Login')
            return redirect('login')
    else:
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})

Upvotes: 0

Views: 3774

Answers (1)

Related Questions