python - Django allauth custom login form not rendering all fields in custom user model -
i trying implement login of consists custom user model. possible inherit allauth.account.forms.loginform
, add custom field custom login form?
idea assign user role @ time of login overriding login() method.
i have followed allauth configuration , mentioned forms use login in settings.py following code
auth_user_model = 'core.user' account_signup_form_class = 'core.forms.signupform' account_forms = {'login': 'core.forms.coreloginform'}
i using no auth backends other django.contrib.auth.backends.modelbackend
, allauth.account.auth_backends.authenticationbackend
. custom signup working fine me without issues. custom loginform not rendering fields in user model. allauth loginform inherited per accepted answer in so post , choicefield added 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 ./manage.py runserver
says module "core.forms" not define "signupform" class
. have defined signupform in core.forms below , signup work if coreloginform inherited forms.form instead of loginform
. if
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 html page. problem here have redefine every methods in class including authenticate()
, perform_login()
etc. end in copying whole loginform , pasting in forms.py of app. dont want think against dry principle. there simple way add custom field custom loginform , override login() method?
tia
probably have solved problem ll leave solution other guys ve spent more 10 minutes on problem me :)
i have found easy way add field allauth login form:
as have done - add settings.py :
account_forms = {'login': 'core.forms.coreloginform'}
and after in forms.py need add :
from django import forms allauth.account.forms import loginform class coreloginform(loginform): def __init__(self, *args, **kwargs): super(coreloginform, self).__init__(*args, **kwargs) ## here add new fields need self.fields["new-field"] = forms.charfield(label='some label', max_length=100)
Comments
Post a Comment