Home>

After registering a user, an error appears in which an account confirmation email does not come to the mail, but the user is displayed in the system

P.S. Disabled 2-Step Verification on the emails I tested with.

views.py

from django.shortcuts import render, redirect
from .forms import RegistrationForm
from .models import Account
from django.contrib import messages, auth
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.contrib.sites.shortcuts import get_current_site
from django.template.loader import render_to_string
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.utils.encoding import force_bytes
from django.contrib.auth.tokens import default_token_generator
from django.core.mail import EmailMessage
def register (request):
    if request.method== 'POST':
        form= RegistrationForm (request.POST)
        if form.is_valid ():
            first_name= form.cleaned_data ['first_name']
            last_name= form.cleaned_data ['last_name']
            phone_number= form.cleaned_data ['phone_number']
            email= form.cleaned_data ['email']
            password= form.cleaned_data ['password']
            username= email.split ("@") [0]
            user= Account.objects.create_user (first_name= first_name, last_name= last_name, email= email, username= username, password= password)
            user.phone_number= phone_number
            user.save ()
            current_site= get_current_site (request)
            mail_subject= 'Please activate your account'
            message= render_to_string ('accounts /account_verification_email.html', {
                'user': user,
                'domain': current_site,
                'uid': urlsafe_base64_encode (force_bytes (user.pk)),
                'token': default_token_generator.make_token (user),
            })
            to_email= email
            send_email= EmailMessage (mail_subject, message, to= [to_email])
            send_email.send ()
            # messages.success (request, 'Thank you for registering with us. We have sent you a verification email to your email adress. Please verify it.')
            return redirect ('/accounts /login /? command-verification &
email= '+ email)
    else:
        form= RegistrationForm ()
    context= {
        'form': form,
    }
    return render (request, 'accounts /register.html', context)
def login (request):
    if request.method== 'POST':
        email= request.POST ['email']
        password= request.POST ['password']
        user= auth.authenticate (email= email, password= password)
        if user is not None:
            auth.login (request, user)
            messages.success (request, 'You are now logged in.')
            return redirect ('dashboard')
        else:
            messages.error (request, 'Invalid login credentials.')
            return redirect ('login')
    return render (request, 'accounts /login.html')
@login_required (login_url= 'login')
def logout (request):
    auth.logout (request)
    messages.success (request, 'You are logged out.')
    return HttpResponseRedirect ('login')
def activate (request, uidb64, token):
    try:
        uid= urlsafe_base64_decode (uidb64) .decode ()
        user= Account._default_manager.get (pk= uid)
    except (TypeError, ValueError, OverflowError, Account.DoesNotExist):
        user= None
    if user is not None and default_token_generator.check_token (user, token):
        user.is_active= True
        user.save ()
        messages.success (request, 'Congratulations! Your account is activated.')
        return redirect ('login')
    else:
        messages.error (request, 'Invalid activation link')
        return redirect ('register')
@login_required (login_url= 'login')
def dashboard (request):
    return render (request, 'accounts /dashboard.html')
def forgotPassword (request):if request.method== 'POST': email= request.POST ['email']
        if Account.objects.filter (email= email) .exists ():
            user= Account.objects.get (email__exact= email)
            current_site= get_current_site (request)
            mail_subject= 'Reset Your Password'
            message= render_to_string ('accounts /reset_password_email.html', {
                'user': user,
                'domain': current_site,
                'uid': urlsafe_base64_encode (force_bytes (user.pk)),
                'token': default_token_generator.make_token (user),
            })
            to_email= email
            send_email= EmailMessage (mail_subject, message, to= [to_email])
            send_email.send ()
            messages.success (request, 'Password reset email has been sent to your email address.')
            return redirect ('login')
        else:
            messages.error (request, 'Account does not exists!')
            return redirect ('forgotPassword')
    return render (request, 'accounts /forgotPassword.html')
def resetpassword_validate (request, uidb64, token):
    try:
        uid= urlsafe_base64_decode (uidb64) .decode ()
        user= Account._default_manager.get (pk= uid)
    except (TypeError, ValueError, OverflowError, Account.DoesNotExist):
        user= None
    if user is not None and default_token_generator.check_token (user, token):
        request.session ['uid']= uid
        messages.success (request, 'Please reset your password')
        return redirect ('resetPassword')
    else:
        messages.error (request, 'This link has been expired!')
        return redirect ('login')
def resetPassword (request):
    if request.method== 'POST':
        password= request.POST ['password']
        confirm_password= request.POST ['confirm_password']
        if password== confirm_password:
            uid= request.session.get ('uid')
            user= Account.objects.get (pk= uid)
            user.set_password (password)
            user.save ()
            messages.success (request, 'Password reset successful!')
            return redirect ('login')
        else:
            messages.error (request, 'Password do not match!')
            return redirect ('resetPassword')
    else:
        return render (request, 'accounts /resetPassword.html')

please attach the stacktrace in text form to the question

Михаил Ребров2022-01-20 02:57:14

added to question

Mike Zigberman2022-01-20 02:57:14

check that you have the right to send messages. Google has such troubles. they need an app password

Za Ars2022-01-20 02:57:14

@ZaArs all rights are there, and access is also open to unsafe applications. There is no confirmation of registration in spam and in remote ones either.

Mike Zigberman2022-01-20 02:57:14

try the procedure from Gmail Part dev.to/abderrahmanemustapha/…

Za Ars2022-01-19 07:19:25