After creating signup for users I am unable to login as admin to django project

(adm) #1

Hi,

I am quite new to Django so hope this one is simple.

After creating user sign up with this tutorial:

I am unable to login to my admin user and the following error pops up:

RelatedObjectDoesNotExist at /admin/login/

User has no profile.

Request Method: POST
Request URL: http://127.0.0.1:8000/admin/login/?next=/admin/
Django Version: 2.2
Exception Type: RelatedObjectDoesNotExist
Exception Value: User has no profile.

My guess is that Django thinks I am normal user without password confirmed.

Maybe it is something to do with this view?:
def signup(request):
if request.method == ‘POST’:
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.is_active = False
user.save()
current_site = get_current_site(request)
subject = ‘Activate Your MySite Account’
message = render_to_string(‘signup/account_activation_email.html’, {
‘user’: user,
‘domain’: current_site.domain,
‘uid’: urlsafe_base64_encode(force_bytes(user.pk)),
‘token’: account_activation_token.make_token(user),
})
user.email_user(subject, message)
return redirect(‘account_activation_sent’)
else:
form = SignUpForm()
return render(request, ‘signup/signup.html’, {‘form’: form})

Anyway, after taking of the app off the INSTALLED APPS and commenting out urls for that app admin is available.

Any guess of what might have happened?

ps. What I also have been doing is checking out each variant of login user as per article commenting out/replacing each parts of code with the new one suggested by the author. :slight_smile:

(Basil Jose) #2

Your model please, do you have mapped user to Profile ?.

1 Like
(Gitsakti) #3

I am not sure but can you try once by setting it to True

(adm) #4

Its not that Gitsaki. Thanks anyway.

Yes, thank you Basil Jose.

After disabling ‘Class Profile’ i am getting logged in. :smile:

just need to read:

on how to extend Django user properly.

Thanks for pointing in the right direction.

What do you mean by ‘mapping user to profile’?

Kind regards

(Basil Jose) #5

Once you create a User object you should create the Profile object with required fields for that user.

(adm) #6

So what should I type please? :slight_smile: You are helping a noob. :slight_smile:

(Nate) #7

Had a similar issue where I was creating a profile for a user but it was not created for the admin user. I solved it by using a signal on the user’s save method to create a profile within models.py. Something like this:

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver

class Profile(models.Model):
field = models.CharField(max_length=5, blank=True, null=True)

Note: blank=True, null=True or a default value is required if you want to create the profile without specifying a value.

@receiver(post_save, sender=User)
def save_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance).save()

Someone may have a better solutions… not a Django wizard by any means.

2 Likes
(adm) #8

Thank you so much wizard Nate! :wink: I still do not know how the hell it is working but it motivates me to do some reading…of Vitor blog of course :slight_smile:

2 Likes