django-allauth: rearrange form fields (change order)

你说的曾经没有我的故事 提交于 2019-12-06 02:30:30

问题


I am trying to use django-allauth for user registrations. I have this form

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

and as per instructions I have set in my settings.py

ACCOUNT_SIGNUP_FORM_CLASS = "userprofile.forms.UserProfileForm"

The problem is that when the form is rendered my custom form asking for gender, country etc is rendered on top and then the standard User fields that ask for username, email and password. Something in this order:

  • gender
  • country
  • city
  • birth_date
  • has_accepted_tos
  • username
  • email
  • password1
  • password2

    I would like to display the fields in this order

  • username

  • email
  • password1
  • password2
  • gender
  • country
  • etc

回答1:


You need to provide the field_order attribute:

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']

However, a better practice would be to use the template file to render the form exactly as you want to.

Quoting the project author:

(...) Then again, I would not put too much weight on what allauth has to offer out of the box display/design wise. All of this is just for reference, the idea is that you create a proper design & form layout in your templates.

References:

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

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



来源:https://stackoverflow.com/questions/20176206/django-allauth-rearrange-form-fields-change-order

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