Re-Order rendering of input fields of django-contact-form?

1,072 views
Skip to first unread message

Tobias Dacoir

unread,
Jan 20, 2015, 4:26:01 PM1/20/15
to django...@googlegroups.com
I'm using django-contact-form which allows me to subclass it and add custom fields to it. However all my added fields will appear at the bottom. I even tried overwriting the original fields but still the order they appear is wrong. How can I control this? I tried searching for an answer, but only found ModelForm info that didn't work as the parent just inherits from forms.Form and is not a ModelForm.

Parent Class cound be found here: https://bitbucket.org/ubernostrum/django-contact-form/src/4b7d2fa20c1d01568fb7c4c800155378e176923b/contact_form/forms.py?at=default

My overwritten class:
class CustomContactForm(ContactForm):
    REASON
= (
       
('support', 'Support'),
       
('feedback','Feedback'),
   
)
    reason
= forms.ChoiceField(choices=REASON, label='Reason')
    name
= forms.CharField(max_length=100,
                           label
=u'Your name')
    email
= forms.EmailField(max_length=200,
                             label
=u'Your email address')
    body
= forms.CharField(widget=forms.Textarea,
                              label
=u'Your message')



and my template:
{% extends 'base.html' %}

{% block body_block %}
<h2>Contact Form</h2>
  <p>To send us a message fill out the below form.</
p>
       
<form method="post">{% csrf_token %}
{{ form.as_p }}
           
<input type="submit" value="Submit">
 
</form>
{% endblock %}


Paul Royik

unread,
Jan 20, 2015, 4:51:10 PM1/20/15
to django...@googlegroups.com
Try
def __init__(self, *args, **kwargs):
        super(CustomContactForm, self).__init__(*args, **kwargs)
        self.fields.keyOrder = ['name', 'reason', 'email', 'body']

Tobias Dacoir

unread,
Jan 21, 2015, 11:49:33 AM1/21/15
to django...@googlegroups.com
Unfortunately this doesn't work as the contact form needs to be passed the request as well. I tried to modify the call to super but it didn't work.

As a workaround what I did now was to look at the HTML Code that the tag {{ form.as_p }} creates, and copy and paste that into the HTML template instead of using {{ form.as_p }}. Then I can freely shuffle around my fields, however for stuff like Choice Widgets, I need to supply the options in the HTML code as well, which is quite troublesome.

So far I still haven't found a real solution. :(

Paul Royik

unread,
Jan 21, 2015, 11:52:56 AM1/21/15
to django...@googlegroups.com
So pass the request

def __init__(self, request, *args, **kwargs):
        self.request=request
        super(CustomContactForm, self).__init__(*args, **kwargs) 

Tobias Dacoir

unread,
Jan 21, 2015, 2:36:59 PM1/21/15
to django...@googlegroups.com
It still complained about the request, but I was finally able to get it to work using this code:
    def __init__(self, request, *args, **kwargs):

       
super(CustomContactForm, self).__init__(request=request, *args, **kwargs)
        fields_keyOrder
= ['name', 'reason', 'email', 'body']
       
if (self.fields.has_key('keyOrder')):
           
self.fields.keyOrder = fields_keyOrder
       
else:
           
self.fields = OrderedDict((k, self.fields[k]) for k in fields_keyOrder)


However it still does not re-order the fields, I searched stackoverflow again and found this for Django 1.7: https://github.com/pennersr/django-allauth/issues/356#issuecomment-24758824
But it still does not work. The order doesn't change it all. sigh :(

Tobias Dacoir

unread,
Jan 21, 2015, 3:00:52 PM1/21/15
to django...@googlegroups.com
Ah it works. After I restarted Django and hit F5 again it re-orders it now. So the code above is the solution.
Thanks!

Aashutosh Rathi

unread,
Nov 5, 2018, 6:17:49 AM11/5/18
to Django users
Yes, this works for me.
Make sure


super(AddLabForm, self).__init__(*args, **kwargs)
       fields_keyOrder = ['id', 'date', 'start_time', 'end_time']
       if 'keyOrder' in self.fields:
           self.fields.keyOrder = fields_keyOrder
       else:
           self.fields = OrderedDict((k, self.fields[k]) for k in fields_keyOrder)

amit pant

unread,
Nov 9, 2018, 10:17:38 PM11/9/18
to django...@googlegroups.com
can we use clean method instead of __init__

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/67309700-3cfc-4199-a789-8bc8653b1022%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages