Django - AttributeError at /signup/ 'User' object has no attribute 'profile'
I implemented 2FA in my Django application however I am getting AttributeError at /signup/ 'User' object has no attribute 'profile'
error when clicking submit in my SignIn form. It shows me that error occurs in six.text_type(user.profile.email_confirmed)
line.
I don't have any custom User model. I am using default User model.
Could you please tell me wjere is the issue and how to fix it?
tokens.py
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.utils import six
class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
def _make_hash_value(self, user, timestamp):
return (
six.text_type(user.pk) + six.text_type(timestamp) +
six.text_type(user.profile.email_confirmed)
)
account_activation_token = AccountActivationTokenGenerator()
views.py
from django.contrib.auth import login
from django.contrib.auth.models import User
from .forms import SignUpForm
from django.views.generic import View
from django.shortcuts import render, redirect
from django.contrib.sites.shortcuts import get_current_site
from django.urls import reverse_lazy
from django.contrib import messages
from django.utils.encoding import force_bytes
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.utils.encoding import force_text
from django.contrib import messages
# Sign Up View
class SignUpView(View):
form_class = SignUpForm
template_name = 'user/register.html'
def get(self, request, *args, **kwargs):
form = self.form_class()
return render(request, self.template_name, {'form': form})
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False # Deactivate account till it is confirmed
user.save()
current_site = get_current_site(request)
subject = 'Activate Your Account'
message = render_to_string('user/account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
})
user.email_user(subject, message)
messages.success(request, ('Please Confirm your email to complete registration.'))
return redirect('login')
return render(request, self.template_name, {'form': form})
class ActivateAccount(View):
def get(self, request, uidb64, token, backend='django.contrib.auth.backends.ModelBackend', *args, **kwargs):
try:
uid = force_text(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.profile.email_confirmed = True
user.save()
login(request, user, backend='django.contrib.auth.backends.ModelBackend')
messages.success(request, ('Your account have been confirmed.'))
return redirect('homepage')
else:
messages.warning(request, ('The confirmation link was invalid, possibly because it has already been used.'))
return redirect('homepage')
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from captcha.fields import ReCaptchaField
# Sign Up Form
class SignUpForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required=False, help_text='Optional')
last_name = forms.CharField(max_length=30, required=False, help_text='Optional')
email = forms.EmailField(max_length=254, help_text='Enter a valid email address')
captcha = ReCaptchaField()
def clean_email(self):
submitted_data = self.cleaned_data['email']
if '@emaildomain.com' not in submitted_data:
raise forms.ValidationError('You must register using a emaildomain address')
return submitted_data
class Meta:
model = User
fields = [
'username',
'first_name',
'last_name',
'email',
'password1',
'password2',
'captcha'
]