覆盖django-allauth默认表单

时间:2013-12-17 10:45:38

标签: django django-forms django-allauth

我想覆盖django-allauth的默认形式,以便更改\添加默认窗口小部件属性,类变量而不更改allauth窗体的初始代码。 我怎么能这样做?

我的工作区:
1)安装django-allauth,作为包装通过pip;
2)根据github项目的建议配置设置;
3)本地创建的模板登录,注册;
4)创建了forms.py,它定义了一个子类形式django-allauth

from allauth.account import SigninForm

class mySigninForm (SigninForm):
     username = ...//override variable

这是正确的方法吗?

谢谢!

2 个答案:

答案 0 :(得分:4)

如果您想使用allauth表单提供的方法或功能,那么覆盖您当前的表单类是最好的选择。

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

如果您需要完全自定义的表单,可以使用自定义django表单,然后在视图中使用它。例如:

形式

from django import forms
from myapp.forms import widgets

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

视图

from django.views.generic.edit import FormView
from myapp.forms import MyLoginForm

LoginUser(FormView):
    form = MyLoginForm

答案 1 :(得分:0)

您可以通过创建自己的模板文件树来覆盖任何(或所有)allauth默认模板。

层次结构的命名必须与allauth匹配,例如这来自我的项目:

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

settings.py中的模板设置必须包含这些目录,例如:

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显示了如何做到这一点。免责声明:我写了那个演示。

相关问题