override django-allauth default forms

后端 未结 2 1307
走了就别回头了
走了就别回头了 2021-02-04 19:42

I want to override the default forms of django-allauth in order to change\\add default widget attributes, class variables without changing the initial code of allauth form. How

2条回答
  •  时光说笑
    2021-02-04 19:53

    If you'd like to use the methods or functionalities provided by the allauth forms then overriding the form classes like you currently are is your best bet.

    from allauth.account.forms import LoginForm
    from django import forms
    from myapp.forms import widgets
    
    
    class MyLoginForm(LoginForm):
    
        // Override attributes
        existing_field = forms.CharField(widget=widgets.CustomWidget())
    
        // Add Custom Attributes
        new_field = forms.CharField(widget=widgets.CustomWidget())
    

    If you want a completely custom form, you can use a custom django form and then use it in the view. For Example:

    forms

    from django import forms
    from myapp.forms import widgets
    
    class MyLoginForm(forms.Form):
        // Add atributes and methods
        some_field = forms.CharField(widget=widgets.CustomWidget())
    

    views

    from django.views.generic.edit import FormView
    from myapp.forms import MyLoginForm
    
    LoginUser(FormView):
        form = MyLoginForm
    

提交回复
热议问题