404 Error when logged into Admin

Hi, I am a beginner and have been learning Django by following a backend tutorial series by Andreas Jud.

Based on Part 11, after registering the signals.py in apps.py, I am getting a 404 error every time I log in to my admin page. Without that ready() in apps.py there’s no error.

Even though I am getting this error, I am able to access the admin manually by entering the /admin URL in the browser.

I have recorded the error in a youtube video



Project’s source code

have you try set AUTH_USER_MODEL in settings.py?

That is required only when I am using a custom user model, right?

I am using just the default User model for auth and a custom model called Profile which has a 1-to-1 relation with the User model to store extra details about the user.

from django.db import models
from django.contrib.auth.models import User
from django.templatetags.static import static


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='avatars/', null=True, blank=True)
    realname = models.CharField(max_length=50, null=True, blank=True)
    email = models.EmailField(unique=True, null=True)
    location = models.CharField(max_length=20, null=True, blank=True)
    bio = models.TextField(null=True, blank=True)
    created = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.user)

I haven’t used One to One Field, so the information is not accurate.

The login itself goes without any problem. Because you wrote your code to use the profile model.
However, Django believes that the problem is caused by comparing the AUTH_USER_MODEL (default user model) and the profile model.

You are login in using a profile model, and Django uses AUTH_USER_MODEL to identify the user.

This problem appears to be caused by using a separate user model and not changing AUTH_USER_MODEL.

And why did you connect it with a one-to-one field instead of abstract user model??

As I mentioned, I am following a YouTube tutorial.

The Profile model is just to connect the extra details for a User through a 1-to-1 relation, so I doubt it is used for login.

I just found the solution for this issue in the next part’s comments of the YouTube tutorial.

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    user = instance
    if created:
        Profile.objects.create(
            user=user,
            email=user.email
        )
    else:
        if not instance.is_superuser: ## new 
            profile = get_object_or_404(Profile, user=user)
            profile.email = user.email
            profile.save()

This if condition fixed the 404 error, but an explanation about the cause of this error would be helpful. P.S No Explanation was given in the YT video.

1 Like

Look at what the error message you posted is telling you:

“No Profile matches the given query”

If you look at your else clause before this new if, you would have had:

else:
    profile = get_object_or_404(Profile, user=user)
    profile.email = user.email
    profile.save()

What’s going to happen if this create_profile signal is triggered on an existing user that doesn’t currently have a Profile object associated with it?

Oh, I get it now, the admin user does’t have a profile attached to it. That’s why “No Profile matches the given query” error was raised. Am I right?

My bad, I completed ignored that error message and assumed this was some URLconf error.

Thanks for the clarity.

Yep, that is exactly it.