0

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})
  • For development purpose you can use [**`Django-doc EmailBackend`**](https://docs.djangoproject.com/en/4.0/topics/email/#email-backends) this [post](https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html) explain how to send email. – Ankit Tiwari May 30 '22 at 06:16