微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

django allauth:在注册表单中添加更多字段

如何解决django allauth:在注册表单中添加更多字段

我安装了 django-allauth 软件包,我正在尝试从我的 User model 添加一些字段 到 allauth

注册表单
from allauth.account.forms import SignupForm
class MyCustomSignupForm(SignupForm):
    class Meta:
        model = User
        fields = ('username','email','mobile','city','district','state','country','pin_code')

但我在 signup/accounts/signup 表单中没有看到许多这些字段。

我该怎么做

解决方法

它对我有用。关于这个主题有几个很好的指南。用您自己的方式覆盖自定义注册表单。这是在 forms.py

中完成的
from allauth.account.forms import SignupForm

class CustomignupForm(SignupForm):
   def __init__(self,*args,**kwargs):
# Call the init of the parent class
    super().__init__(*args,**kwargs)
    self.fields[_("mobile")] = forms.CharField(required=True)
    self.fields[_("city")] = forms.CharField(required=True)
    self.fields[_("district")] = forms.CharField(required=True)
    #"Rest of your fields"
# Put in custom signup logic
   def custom_signup(self,request,user):
    # Set the user's type from th form reponse
    user.mobile= self.cleaned_data[_("mobile")]
    user.city= self.cleaned_data[_("city")]
  #"Rest of your fields"

    user.save()
    return user

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。