如何使用另一个字段登录Django Allauth? [英] How to use another field for logging in with Django Allauth?

查看:60
本文介绍了如何使用另一个字段登录Django Allauth?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功设置了django-allauth以及自定义用户模型,该模型使用户可以直接使用电子邮件和密码或通过Facebook登录,在这种情况下,电子邮件是从Facebook提取并保存在自定义用户模型的电子邮件"字段中.我还创建了一个移动字段,到目前为止,该字段一直为空.

I have successfully setup django-allauth along with a custom user model which let's users sign in directly using email and password or through Facebook, in which case email is taken from Facebook and saved in the Email field of the custom user model. I have also created a mobile field which stays empty as of now.

我想允许用户使用他们的Facebook,电子邮件或手机登录.不幸的是,该字段在模型中不是unique=True.我曾想过要捕获移动设备并获取关联的电子邮件地址,然后将其与密码一起使用来登录任何用户.

I want to allow users to log in using their Facebook, Email or MOBILE. Unfortunately that field is not unique=True in the model. I have thought of capturing the mobile and fetching the associated email address and then use that along with the password to log in any user.

但是,我不知道如何扩展django-allauth随附的SIGN IN表单或在用户可以更改它的位置满足我需要的代码块.

However, I don't know how to extend the SIGN IN form that comes with django-allauth or the block of code that signs a user in where I can change it to meet my need.

到目前为止,我没有找到与此问题相关的任何当前代码,但是如果有帮助,我愿意一提就提供.

As of now I don't find any of my current code relevant to this problem, but if it helps I am willing to provide it upon mention.

推荐答案

以下解决方案对我有用.我将代码放在forms.py文件中.

The following solution worked for me. I put the code in the forms.py file.

from allauth.account.forms import LoginForm
from auth_project import settings

class CustomLoginForm(LoginForm):
    def __init__(self, *args, **kwargs):
        super(LoginForm, self).__init__(*args, **kwargs)
        if settings.ACCOUNT_AUTHENTICATION_METHOD == "email":
            login_widget = forms.TextInput(attrs={'type': 'text',
                                                  'placeholder':
                                                  ('Mobile Number'),
                                                  'autofocus': 'autofocus'})
            login_field = forms.CharField(label=("Mobile"),
                                           widget=login_widget)
        self.fields["login"] = login_field
        set_form_field_order(self,  ["login", "password", "remember"])

    def user_credentials(self):
        credentials = {}
        mobile = self.cleaned_data["login"]
        login = CustomUser.objects.filter(mobile=mobile).values('email')[0]['email']
        if settings.ACCOUNT_AUTHENTICATION_METHOD == "email":
            credentials["email"] = login
        credentials["password"] = self.cleaned_data["password"]
        return credentials

# this is to set the form field in order
# careful with the indentation
def set_form_field_order(form, fields_order):
    if hasattr(form.fields, 'keyOrder'):
        form.fields.keyOrder = fields_order
    else:
        # Python 2.7+
        from collections import OrderedDict
        assert isinstance(form.fields, OrderedDict)
        form.fields = OrderedDict((f, form.fields[f])
                                  for f in fields_order)

谢谢.

这篇关于如何使用另一个字段登录Django Allauth?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆