Django allauth自定义登录表单未在自定义用户模型中呈现所有字段 [英] Django allauth custom login form not rendering all fields in custom user model

查看:130
本文介绍了Django allauth自定义登录表单未在自定义用户模型中呈现所有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现包含自定义用户模型的登录。是否可以从 allauth.account.forms.LoginForm 继承并将自定义字段添加到自定义登录表单?

I am trying to implement login of that consists a custom user model. Is it possible to inherit from allauth.account.forms.LoginForm and add a custom field to the custom login form?


想法是通过覆盖
login()方法在登录时为用户分配角色。

Idea is to assign user with a role at the time of login by overriding login() method.

我遵循了 allauth配置,并提到了使用以下代码登录settings.py的方式

I have followed allauth configuration and mentioned what forms to use for login in settings.py with following code

AUTH_USER_MODEL = 'core.User'
ACCOUNT_SIGNUP_FORM_CLASS = 'core.forms.SignupForm'
ACCOUNT_FORMS = {'login': 'core.forms.CoreLoginForm'}

我没有使用除 django.contrib.auth.backends.ModelBackend allauth.account.auth_backends.AuthenticationBackend 。自定义注册对我来说很好,没有任何问题。但是 自定义登录表单不会呈现用户模型中的所有字段 。按照此 SO Post 中接受的答案,Allauth LoginForm被继承。

I am using no auth backends other than django.contrib.auth.backends.ModelBackend and allauth.account.auth_backends.AuthenticationBackend. custom signup is working fine for me without any issues. But custom loginform is not rendering all fields in the user model. Allauth LoginForm was inherited as per accepted answer in this SO Post and a choicefield was added to the custom login form.

from allauth.account.forms import LoginForm
class CoreLoginForm(LoginForm):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(CoreLoginForm, self).__init__(*args, **kwargs)
    role = forms.ChoiceField(widget=forms.Select(), choices=User.roles, initial=User.roles[0])

上。/manage.py运行服务器它表示 Module core.forms未定义 SignupForm类。我已经在core.forms中定义了一个SignupForm,如下所示,如果CoreLoginForm是从form.Form而不是LoginForm 继承的,则 signup将起作用。因此,如果我这样做

Upon a ./manage.py runserver it says Module "core.forms" does not define a "SignupForm" class. I have already defined a SignupForm in core.forms as below and signup will work if CoreLoginForm is inherited from forms.Form instead of LoginForm. So if I do

class CoreLoginForm(forms.Form):
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(CoreLoginForm, self).__init__(*args, **kwargs)

    role = forms.ChoiceField(widget=forms.Select(), choices=User.roles, initial=User.roles[0])

我可以将自定义登录表单呈现到html页面。但是这里的问题是我必须重新定义类中的每个方法,包括 authenticate() perform_login()等。最后将复制整个LoginForm并将其粘贴到应用程序的forms.py中。我不想这样做,因为我认为这违反了DRY原则。是否有一种简单的方法可以将自定义字段添加到自定义loginform并覆盖login()方法?

I can render custom login form to html page. But problem here is that I have to redefine every methods in the class including authenticate(), perform_login() etc. This will end up in copying whole LoginForm and pasting it in forms.py of the app. I dont want to do this as I think this is against DRY principle. Is there a simple way to add a custom field to custom loginform and override login() method?

TIA

推荐答案

可能您已经解决了您的问题,但我将把这个解决方案留给像我这样花费超过10分钟的其他人来解决:)

Probably you have solved your problem already but i ll leave this solution for other guys who ve spent more then 10 minutes on this problem like me :)

我找到了一种向Allauth登录表单添加字段的简单方法:

I have found an easy way to add a field to Allauth login form:

完成后-添加到settings.py:

As you have done - add to settings.py :

ACCOUNT_FORMS = {'login': 'core.forms.CoreLoginForm'}

,然后在您的form.py中添加:

and after that in your forms.py you need to add :

from django import forms
from allauth.account.forms import LoginForm

class CoreLoginForm(LoginForm):

    def __init__(self, *args, **kwargs):
        super(CoreLoginForm, self).__init__(*args, **kwargs)
        ## here i add the new fields that i need
        self.fields["new-field"] = forms.CharField(label='Some label', max_length=100)

这篇关于Django allauth自定义登录表单未在自定义用户模型中呈现所有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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