django-allauth:重新排列表单字段(更改顺序) [英] django-allauth: rearrange form fields (change order)
问题描述
我正在尝试使用django-allauth进行用户注册。我有这个表格
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')
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
国家
城市
出生日期
已接受_tos
用户名
电子邮件
password1
password1
password2
password2
我想以此顺序显示字段
用户名
电子邮件
password1
password1
密码2
性别
国家/地区
等
推荐答案
您需要提供 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']
但是,更好的做法是使用模板文件完全按照您想要的方式呈现表单。
However, a better practice would be to use the template file to render the form exactly as you want to.
引用项目作者:
(...)再说一遍,我不会对allauth提供的
开箱即用的显示权重过多/明智的设计。所有这些仅供
参考,其想法是您创建适当的设计&模板中的表单布局
。
(...) 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.
参考文献:
[1] https://github.com/pennersr/ django-allauth / blob / master / allauth / account / forms.py
[2] https://github.com/pennersr/django-allauth/issues/269
这篇关于django-allauth:重新排列表单字段(更改顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!