Home > Net >  AttributeError: 'User' object has no attribute 'user' while trying to verify use
AttributeError: 'User' object has no attribute 'user' while trying to verify use

Time:09-12

I have django web app with authentication and email verification. I have two options of authentication. One is authenticating with Customer or with Employee. I have User(AbstractUser) to connect them both.

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    is_customer = models.BooleanField(default=False)
    is_employee = models.BooleanField(default=False)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)

    signup_confirmation = models.BooleanField(default=False)

class Customer(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
    phone_number = models.CharField(max_length=20)
    location = models.CharField(max_length=20)

class Employee(models.Model):
    user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
    phone_number = models.CharField(max_length=20)
    designation = models.CharField(max_length=20)

views



...employee_register...

class customer_register(CreateView):
    model = User
    form_class = CustomerSignUpForm
    template_name = '../templates/customer_register.html'

    def form_valid(self, form):

        user = form.save()
        # current_site = get_current_site(request)
        current_site = '127.0.0.1:8000'
        subject = 'Please Activate Your Account'
        message = render_to_string('activation_request.html', {
            'user': user,
            'domain': current_site,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'token': account_activation_token.make_token(user),
        })
        send_mail(
            subject,
            message,
            '[email protected]',
            ['[email protected]'],
            fail_silently=False,
        )
        # login(self.request, user)
        return redirect('/')


def activate(request, uidb64, token):
    try:
        uid = force_str(urlsafe_base64_decode(uidb64))
        user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and account_activation_token.check_token(user, token):
        user.is_active = True
        user.user.signup_confirmation = True
        user.save()
        login(request, user)
        return redirect('index')
    else:
        return render(request, 'activation_invalid.html')

In my views first I have class which is triggered when submit button is pressed in my form. Then email with link to url which triggers activate function is sent.

activation_request.html

{% autoescape off %}
Hi {{ user.username }},

Please click the following link to confirm your registration:

http://{{ domain }}{% url 'activate' uidb64=uid token=token %}
{% endautoescape %}

urls

...
path('customer_register/',views.customer_register.as_view(), name='customer_register'),
path('activate/<slug:uidb64>/<slug:token>/', views.activate, name='activate'),

In my activate function I want to set BooleanField in my customer model to true with user.user.signup_confirmation = True but it doesn't work.

I get an error:

AttributeError: 'User' object has no attribute 'user'.

CodePudding user response:

user object has no attribute named user because you haven't assigned that attribute.

Solution :

write in the activate view :

user.signup_confirmation = True

instead of :

user.user.signup_confirmation = True
  • Related