Django allauth custom login form not rendering all fields in custom user model

这一生的挚爱 提交于 2019-12-23 02:46:11

问题


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?

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

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'}

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])

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])

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


回答1:


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 :)

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

As you have done - add to settings.py :

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

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)


来源:https://stackoverflow.com/questions/37432492/django-allauth-custom-login-form-not-rendering-all-fields-in-custom-user-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!