override django-allauth default forms

空扰寡人 提交于 2019-12-03 00:42:42

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

You can override any (or all) of the allauth default templates by creating your own tree of template files.

The naming of the hierarchy must match allauth, e.g. this is from my project:

allauthdemo/templates/
├── allauth
│   ├── account
│   └── socialaccount
├── my
├── other
├── template
└── dirs

The template settings in settings.py must include those dirs, e.g.:

TEMPLATES = [
    {
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        os.path.join(BASE_DIR, 'allauthdemo', 'templates', 'allauth'),
        os.path.join(BASE_DIR, 'allauthdemo', 'templates'),
    ],
    ...

This allauth demo on github shows how to do it. Disclaimer: I wrote that demo.

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