简体   繁体   中英

How to Login with Gmail account in Django

I am having a Login button with Gmail account in Django....Here when I am login with Gmail account means I want extra add information from the user and I want to Store that Information in the auth_user table. Please Help me

If I login with Gmail means I will Redirect to that Register Page With Some More Extra Informations. It will Successfully Login but I don't Know How to Save the Extra Information in the auth_user table.Here I am having Extra Register Form with Extra Information to add User but I don't Know how to save that Information in auth_user table in Postgresql.Please Help me I am stuck here Please Help me someone

to solve your issue you need to create a costum user model here is on of my models as an example:

in your model.py file add this :

class CustomAccountManager(BaseUserManager):

    def create_superuser(self, email, username, password, **other_fields):

        other_fields.setdefault('is_staff', True)
        other_fields.setdefault('is_superuser', True)
        other_fields.setdefault('is_active', True)

        if other_fields.get('is_staff') is not True:
            raise ValueError(
                'Superuser must be assigned to is_staff=True.')
        if other_fields.get('is_superuser') is not True:
            raise ValueError(
                'Superuser must be assigned to is_superuser=True.')

        return self.create_user(email, username, password, **other_fields)

    def create_user(self, email, username, password, **other_fields):

        if not email:
            raise ValueError(_('You must provide an email address'))

        email = self.normalize_email(email)
        user = self.model(email=email, username=username, **other_fields)
        user.set_password(password)
        user.save()
        return user


class Account(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(_('email address'), unique=True)
    username = models.CharField(_('username'),max_length=50, unique=True)
    first_name = models.CharField(max_length=150, blank=True)
    last_name = models.CharField(_('Last name'),max_length=50,blank=True)
    Phone =models.CharField(_('Phone Number'),max_length=10,blank=True) 
    City = models.CharField(max_length=10,blank=True)
    address = models.CharField(_('Address'), max_length=150, blank=True)
    
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_admin_province = models.BooleanField(default=False) 
    objects = CustomAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']
    def __str__(self):
        return self.username

and then to be able to add user from django admin add this code to your admin.py :

class UserAdminConfig(UserAdmin):
    model = Account
    search_fields = ('email','City')
    list_filter = ('is_active', 'is_staff')
    list_display = ('email', 'username',
                    'is_active', 'is_staff')
    ordering = ('-id',)
    fieldsets = (
        (None, {'fields': ('email','username','password')}),
        ('Permissions', {'fields': ('is_staff', 'is_active')}),
        ('Personal', {'fields': ('first_name','last_name','Phone','City','address',)}),
    )
    formfield_overrides = {
        Account.address: {'widget': Textarea(attrs={'rows': 50, 'cols': 400})},
        Account.province: {'widget': TextInput },
    }
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email','username','password1', 'password2', 'is_active', 'is_staff')}
         ),
    )


admin.site.register(Account, UserAdminConfig)

i know i have not provided much details but you can check django doc for more info and in case you got stuck i'll be happy to help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM