Answers for "django register and login using email"

0

django authenticate with email

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend

class EmailBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        UserModel = get_user_model()
        try:
            user = UserModel.objects.get(email=username)
        except UserModel.DoesNotExist:
            return None
        else:
            if user.check_password(password):
                return user
        return None
Posted by: Guest on August-20-2020
0

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})
Posted by: Guest on May-28-2020

Code answers related to "django register and login using email"

Python Answers by Framework

Browse Popular Code Answers by Language