How does Django Know the Order to Render Form Fields?

后端 未结 14 1448
不知归路
不知归路 2020-11-28 05:21

If I have a Django form such as:

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = fo         


        
14条回答
  •  生来不讨喜
    2020-11-28 05:50

    With Django >= 1.7 your must modify ContactForm.base_fields as below:

    from collections import OrderedDict
    
    ...
    
    class ContactForm(forms.Form):
        ...
    
    ContactForm.base_fields = OrderedDict(
        (k, ContactForm.base_fields[k])
        for k in ['your', 'field', 'in', 'order']
    )
    

    This trick is used in Django Admin PasswordChangeForm: Source on Github

提交回复
热议问题