gpt4 book ai didi

django - 覆盖 django-allauth 默认表单

转载 作者:行者123 更新时间:2023-12-04 20:09:23 24 4
gpt4 key购买 nike

我想覆盖 django-allauth 的默认形式,以便在不更改 allauth 形式的初始代码的情况下更改\添加默认小部件属性、类变量。
我怎样才能做到这一点?

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

从 allauth.account 导入 SigninForm

类 mySigninForm (SigninForm):
用户名 = ...//覆盖变量

这是正确的方法吗?

谢谢!

最佳答案

如果您想使用 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 表单,然后在 View 中使用它。例如:

形式
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

关于django - 覆盖 django-allauth 默认表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20632061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com