django-allauth:重新排列表单字段(更改顺序)

vog*_*ger 5 forms django django-allauth

我正在尝试使用django-allauth进行用户注册.我有这个表格

class UserProfileForm(forms.ModelForm):
class Meta:
    model = UserProfile
    fields = ('gender', 'country', 'city', 'birth_date', 'has_accepted_tos')
Run Code Online (Sandbox Code Playgroud)

并根据我在settings.py中设置的说明

ACCOUNT_SIGNUP_FORM_CLASS = "userprofile.forms.UserProfileForm"
Run Code Online (Sandbox Code Playgroud)

问题是,当表单呈现我的自定义表单要求性别,国家等呈现在顶部,然后标准用户字段,要求用户名,电子邮件和密码.这个顺序的东西:

  • 性别
  • 国家
  • 生日
  • has_accepted_tos
  • 用户名
  • 电子邮件
  • 密码1
  • 密码2

    我想按此顺序显示字段

  • 用户名

  • 电子邮件
  • 密码1
  • 密码2
  • 性别
  • 国家
  • 等等

bla*_*aze 6

您需要提供以下field_order属性:

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ('gender', 'country', 'city', 'birth_date', 'has_accepted_tos')
        field_order = ['username', 'email', 'password1', 'password2', 'gender', 'country']
Run Code Online (Sandbox Code Playgroud)

但是,更好的做法是使用模板文件完全按照您的意愿呈现表单.

引用项目作者:

(...)然后,我不会过分强调allauth提供的开箱即用显示/设计.所有这些仅供参考,我们的想法是在模板中创建适当的设计和表单布局.

参考文献:

[1] https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py

[2] https://github.com/pennersr/django-allauth/issues/269