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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With