RelatedObjectDoesNotExist at /activate (User has no profile) (Solved)

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!

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    email_verified = models.BooleanField(default=False)

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:

@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):

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']

def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            messages.success(request, f'Your account has been created! Please verify your email.')
            return redirect('verify')
        form = UserRegisterForm()
    return render(request, 'users/register.html', {'form': form})

def activate(request, uidb64, token):
    User = get_user_model()
        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
        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')
        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 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 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.

First, I wouldn’t use a signal for this. I’d create the Profile object directly in your view after saving the form. (See Django Anti-Patterns: Signals | Lincoln Loop for a well-written opinion on this topic - an opinion with which I agree.)

When using Signals with the @receiver decorator in a separate file, you need to import your signals module. Have you done that?

As a side note, when you post code snippets, please insert them between lines containing only 3 backticks - ```. (Make sure you use the backtick - ` and not the apostrophe - '.) That means you would have one line with the 3 backticks, your code, and then one more line with just the 3 backticks. That allows this forum software to format your code nicely:

# The line above this is just ```
def function(parm):
    return parm
# The line after this is just ```

If you want, you can edit your post to put the backticks before and after the code.

Thank you for your help. I have fixed the code, and I’m going to try removing the signal and creating the profile in the view instead. Tried this, no luck. I’ll give the opinion a read, I appreciate that. Turns out I had somehow gotten rid of the line super(Profile, self).save(*args, **kwargs)`` in save()``` and the profile was never saving. I fixed this and now it works perfectly. Here is the updated code:

if form.is_valid():
            u =
            profile = Profile.objects.create(user=u)

By the way, to fix my project for the users whose profile didn’t get created, I created 2 from the command line. Here’s how I did it with python shell

>>> user = get_user_model().objects.get(username='gary')
>>> user
<User: gary>
>>> from users.models import Profile
>>> profile = Profile.objects.create(user=user)
>>> profile
<Profile: gary Profile>
>>> user = get_user_model().objects.get(username='csbooster')
>>> user
<User: csbooster>
>>> profile = Profile.objects.create(user=user)
>>> profile
<Profile: csbooster Profile>
1 Like