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

查看:11
本文介绍了如何使用另一个字段使用 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 附带的登录表单或用于对用户进行签名的代码块,我可以在其中更改它以满足我的需要.

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天全站免登陆