I am trying to create a basic site with a few simple features including a one-to-one extension of the User model in a user profile. I am getting an error that says RelatedObjectDoesNotExist at /activate… (User has no profile). The line that fails is user.profile.email_verified = True . The property doesn’t seem to be working properly. I am a beginner so this may be my mistake, and I would really appreciate some help. I have put code, including relevant views, models, forms, and more, below. Thanks for the help!
# models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
email_verified = models.BooleanField(default=False)
# signals.py
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
# forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
from captcha.fields import ReCaptchaField
class UserRegisterForm(UserCreationForm):
email = forms.EmailField()
captcha = ReCaptchaField()
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
# views.py
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
send_verification_email(username)
messages.success(request, f'Your account has been created! Please verify your email.')
return redirect('verify')
else:
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
def activate(request, uidb64, token):
User = get_user_model()
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.profile.email_verified = True
user.save()
auth_login(request, user)
messages.success(request, f'Thank you for your email confirmation. You are now logged in to your account.')
return redirect('explore')
else:
messages.success(request, f'Your activation link has expired. Please request a new activation link.')
return redirect('verify')
The user profile isn’t created at all, and it should be because the signals.py is creating it. I don’t know what’s causing the issue, but this should be all of the code that is relevant. I know the rest of my code is working, and I am sure that it’s a problem with the profile not being created. I shouldn’t have to manually create the profile when the user is created, with the signals.py. This doesn’t seem to work either. If anyone can point me in the right direction I would greatly appreciate the help. I have looked for help online with no success so far.