Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-allauth: rearrange form fields (change order)

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

like image 216
voger Avatar asked Nov 24 '13 15:11

voger


1 Answers

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

like image 106
blaze Avatar answered Nov 06 '22 09:11

blaze