email verification django code example

Example 1: django email confirmation

from django.http import HttpResponsefrom django.shortcuts import render, redirectfrom django.contrib.auth import login, authenticatefrom .forms import SignupFormfrom django.contrib.sites.shortcuts import get_current_sitefrom django.utils.encoding import force_bytes, force_textfrom django.utils.http import urlsafe_base64_encode, urlsafe_base64_decodefrom django.template.loader import render_to_stringfrom .tokens import account_activation_tokenfrom django.contrib.auth.models import Userfrom django.core.mail import EmailMessagedef signup(request):    if request.method == 'POST':        form = SignupForm(request.POST)        if form.is_valid():            user = form.save(commit=False)            user.is_active = False            user.save()            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,                'uid':urlsafe_base64_encode(force_bytes(user.pk)),                'token':account_activation_token.make_token(user),            })            to_email = form.cleaned_data.get('email')            email = EmailMessage(                        mail_subject, message, to=[to_email]            )            email.send()            return HttpResponse('Please confirm your email address to complete the registration')    else:        form = SignupForm()    return render(request, 'signup.html', {'form': form})

Example 2: User signup with email, mobile, name, password fields using email confirmation django

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 = form.save(commit=False)            
      user.is_active = False            
      user.save()            
      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,                
        'uid':urlsafe_base64_encode(force_bytes(user.pk)),                
        'token':account_activation_token.make_token(user),            
      })            
      to_email = form.cleaned_data.get('email')            
      email = EmailMessage(mail_subject, message, to=[to_email])            
      email.send()            
      return HttpResponse('Please confirm your email address to complete the registration')    
    else:        
      form = SignupForm()    
      return render(request, 'signup.html', {'form': form})

Example 3: django email verification

I figured out a solution , but for the second requirement user has to input the password at the time of account creation . The main goal was to verify the user supplied email.

Models
class Yourmodel(models.Model):
    first_name = models.CharField(max_length=200)
    second_name = models.CharField(max_length=200)
    email = models.EmailField(max_length=100)

Example 4: email validation using django

from django.db import models

class MyModel(models.Model):
    even_field = models.IntegerField(validators=[validate_even])

Example 5: email validation using django

from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

def validate_even(value):
    if value % 2 != 0:
        raise ValidationError(
            _('%(value)s is not an even number'),
            params={'value': value},
        )