AttributeError: 'AnonymousUser' object has no attribute 'is_email_verified'

I have django app with registration system in it and it doesn't work as expected. I recently added email verification in my view and because of that I get error.

For email verification I have 2 functions. One function sends email, the other verifies and creates user.

function 1

from .utils import (
def send_action_email(user, request):
    current_site = get_current_site(request)
    email_subject = 'Activate account | Zane'
    email_body = render_to_string('authentication/email/email_body.html', {
        'user': models.PersonalUser,
        'domain': current_site,
        'uid': urlsafe_b64encode(force_bytes(,
        'token': generate_token.make_token(user)

    email = EmailMessage(subject=email_subject, body=email_body, from_email=settings.EMAIL_FROM_USER, to=[])

function 2

from .utils import (
def activate_user(request, uidb64, token):

        # uid = force_str(urlsafe_base64_decode(uidb64).decode())
        uid = force_str(urlsafe_b64decode(uidb64))

        user = models.PersonalUser.objects.get(pk=uid)
    except Exception as e:
        user = None

    if user and generate_token.check_token(user, token):
        user.is_email_verified = True
        print('user activated')
        return redirect(reverse('login'))
    return render(request, 'authentication/email/activation-failed.html')

Then I have my main signup class:

class PersonalRegister(View):

    def post(self, request, *args, **kwargs):
        #get data from form using request.POST.get

        send_action_email(request.user, request)

    def get(self, request, *args, **kwargs):
        return render(request, 'authentication/personal/personal_signup.html')

from django.urls import path

from .views import (

urlpatterns = [
    path('login/', Login.as_view(), name='login'),
    path('register/', PersonalRegister.as_view(), name='register'),
    path('logout/', logout_user, name='logout'),
    path('activate-user/<uidb64>/<token>/', activate_user, name='activate')

from time import time
from django.contrib.auth.tokens import PasswordResetTokenGenerator

import six

class TokenGenerator(PasswordResetTokenGenerator):
    def _make_hash_value(self, user, timestamp):
        return (six.text_type(

generate_token = TokenGenerator()

When I run my app I get error AttributeError: 'AnonymousUser' object has no attribute 'is_email_verified'.

What have I tried?

Logging in before doing authentication proccess and It worked but point is to make new user logged out. Later I am also planning on restricting access to registration and log in view to logged in users

Thanks for help:)

Back to Top