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

**URL:** <https://forum.djangoproject.com/t/relatedobjectdoesnotexist-at-activate-user-has-no-profile-solved/6930>\
**Category:** Mystery Errors\
**Created:** [March 8, 2021, 9:23am UTC](https://forum.djangoproject.com/t/relatedobjectdoesnotexist-at-activate-user-has-no-profile-solved/6930 "2021-03-08T09:23:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasperholton](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jasperholton/32/3757_2.png) [@jasperholton](https://forum.djangoproject.com/u/jasperholton)\
**Post date:** [March 8, 2021, 9:23am UTC](https://forum.djangoproject.com/t/relatedobjectdoesnotexist-at-activate-user-has-no-profile-solved/6930/1 "2021-03-08T09:23:12Z")

</div>

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!

```auto
# 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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 8, 2021, 2:36pm UTC](https://forum.djangoproject.com/t/relatedobjectdoesnotexist-at-activate-user-has-no-profile-solved/6930/2 "2021-03-08T14:36:46Z")

</div>

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](https://lincolnloop.com/blog/django-anti-patterns-signals/) for a well-written opinion on this topic - an opinion with which I agree.)

When using [Signals with the `@receiver` decorator](https://docs.djangoproject.com/en/3.1/topics/signals/#connecting-receiver-functions) in a separate signals.py 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:

````auto
# 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.

---

<div class="post-metadata">

**Author:** ![jasperholton](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/jasperholton/32/3757_2.png) [@jasperholton](https://forum.djangoproject.com/u/jasperholton)\
**Post date:** [March 8, 2021, 7:48pm UTC](https://forum.djangoproject.com/t/relatedobjectdoesnotexist-at-activate-user-has-no-profile-solved/6930/3 "2021-03-08T19:48:53Z")

</div>

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:

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

```

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 manage.py shell`

```auto
>>> 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>
>>> profile.save()
>>> user.save()
>>> user = get_user_model().objects.get(username='csbooster')
>>> user
<User: csbooster>
>>> profile = Profile.objects.create(user=user)
>>> profile
<Profile: csbooster Profile>
>>> user.save()
>>> profile.save()

```

---

<div class="post-metadata">

**Author:** ![Dadduu2001](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dadduu2001/32/18693_2.png) [@Dadduu2001](https://forum.djangoproject.com/u/Dadduu2001)\
**Post date:** [January 25, 2024, 5:21pm UTC](https://forum.djangoproject.com/t/relatedobjectdoesnotexist-at-activate-user-has-no-profile-solved/6930/4 "2024-01-25T17:21:46Z")

</div>

You can add ```  
dispatch\_uid=‘save\_new\_user\_profile’

```auto
in your both receiver then it will work;
eg: @receiver(post_save, sender=User, dispatch_uid='save_new_user_profile')
```
