I’m not sure about how to go about debugging this. I’ll try and explain as well as I can but if you need additional information then please let me know.
I have a custom user model like so:
class CustomUser(AbstractUser):
email = models.EmailField(unique=True, blank=False, null=False)
email_verified = models.BooleanField(default=False)
email_verify_code = models.UUIDField(editable=False, null=True, blank=True)
email_verify_code_expired = models.DateTimeField(null=True, blank=True)
and in my custom login view I have this:
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(self.request, username=username, password=password)
when I try and do:
user.email_verified
it says that the email_verified field does not exist.
I noticed that PyCharm reports the user model as being an AbstractBaseUser but I have no idea why.
Can someone point me in the right direction please?
Please provide the full context where you’re trying to use this - what file / function / module?
AbstractUser
is a descendant class of AbstractBaseUser
.
Here is my login view:
class CustomLoginView(FormView):
template_name = 'registration/login.html'
form_class = AuthenticationForm
success_url = reverse_lazy('user:account') # Redirect to the account page after login
def form_valid(self, form):
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(self.request, username=username, password=password)
if user is not None:
if user.email_verified is False:
return render(self.request, 'registration/login.html', {
'form': form,
'error': 'Email not verified. Please check your email for verification link.'
})
else:
login(self.request, user)
return redirect(self.get_success_url())
else:
return self.form_invalid(form)
What is your AuthenticationForm
?
Just the default one:
django.contrib.auth.forms.AuthenticationForm
Did you set the AUTH_USER_MODEL
setting in your settings.py
?
Yes I did. I just double checked.
Side note: I hope your posting of the CustomLoginView
is not exactly as it exists in your source file - your form_valid
method is not indented within your CustomLoginView
class.
Please post your complete settings.py file.
Hi, thank you so much for your help. I rejigged things so now I call the verification code from a login signal and that sorted the problem out.
Also yeah the indentation things was my bad when pasting the code here.