Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Correctly Combine Django-Allauth and Custom User Profile App?

I've created a new app called users with the model Profile. For authentication I'm using django-allauth with Facebook and Google providers. Once user is logged in, I'd like to create a profile with some additional information populated from social providers, like: full_name, email, picture.

Here is what I have in the models.py:

from django.contrib.auth.models import User
from django.dispatch import receiver
from allauth.account.signals import user_signed_up


class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    full_name = models.CharField(default=None, max_length=255)
    email = models.CharField(default=None, max_length=500)
    picture = models.ImageField(default='default.jpg', upload_to='profile_pics')

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

    @receiver(user_signed_up)
    def populate_profile(sociallogin, user, **kwargs):

        user.profile = Profile()

        if sociallogin.account.provider == 'facebook':
            user_data = user.socialaccount_set.filter(provider='facebook')[0].extra_data
            picture_url = "http://graph.facebook.com/" + sociallogin.account.uid + "/picture?type=large"
            email = user_data['email']
            full_name = user_data['name']

        if sociallogin.account.provider == 'google':
            user_data = user.socialaccount_set.filter(provider='google')[0].extra_data
            picture_url = user_data['picture']
            email = user_data['email']
            full_name = user_data['name']

        user.profile.picture = picture_url
        user.profile.email = email
        user.profile.full_name = full_name
        user.profile.save()

While logging with Facebook, I'm getting the following error message:

[WinError 10061] No connection could be made because the target machine actively refused it

And when I try to log in with Google, I receive the following:

DataError at /accounts/google/login/callback/ value too long for type character varying(100)

Can someone please tell me what's wrong with my code? Thanks in advance.

like image 869
Android_Minsky Avatar asked Sep 17 '25 05:09

Android_Minsky


1 Answers

Issues are now solved. First error message I solved by adding:

ACCOUNT_EMAIL_VERIFICATION = None

And for the second one I had to add max_length=255 for the picture:

picture = models.ImageField(default='default.jpg', upload_to='profile_pics', max_length=255)
like image 191
Android_Minsky Avatar answered Sep 19 '25 21:09

Android_Minsky