Discussion:
Re-Order rendering of input fields of django-contact-form?
Tobias Dacoir
2015-01-20 21:26:01 UTC
Permalink
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 %}
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/725a9615-1d66-430a-8dbd-c7b1e70a7920%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Paul Royik
2015-01-20 21:51:10 UTC
Permalink
Try

def __init__(self, *args, **kwargs):
super(CustomContactForm, self).__init__(*args, **kwargs)
self.fields.keyOrder = ['name', 'reason', 'email', 'body']
Post by Tobias Dacoir
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.
https://bitbucket.org/ubernostrum/django-contact-form/src/4b7d2fa20c1d01568fb7c4c800155378e176923b/contact_form/forms.py?at=default
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')
{% 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 %}
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6b0ae083-59df-48d6-bc29-f6f9ae6878af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tobias Dacoir
2015-01-21 16:49:33 UTC
Permalink
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. :(
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/080a9ff8-2a05-4af6-aba6-075530e40f8c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Paul Royik
2015-01-21 16:52:56 UTC
Permalink
So pass the request
def __init__(self, request, *args, **kwargs):

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

self.fields.keyOrder = ['name', 'reason', 'email', 'body']
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f0dea9b0-9269-430f-8dd1-5cf20fb3a64d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tobias Dacoir
2015-01-21 19:36:59 UTC
Permalink
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 :(
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f9ad446d-432d-40bd-90c7-7bbb9d484b5a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Tobias Dacoir
2015-01-21 20:00:52 UTC
Permalink
Ah it works. After I restarted Django and hit F5 again it re-orders it now.
So the code above is the solution.
Thanks!
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/49953ca8-9cf5-4eae-acfa-ecfc40f4ad44%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Aashutosh Rathi
2018-11-05 07:24:52 UTC
Permalink
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)
Post by Tobias Dacoir
It still complained about the request, but I was finally able to get it to
super(CustomContactForm, self).__init__(request=request, *args, **
kwargs)
fields_keyOrder = ['name', 'reason', 'email', 'body']
self.fields.keyOrder = fields_keyOrder
self.fields = OrderedDict((k, self.fields[k]) for k in
fields_keyOrder)
However it still does not re-order the fields, I searched stackoverflow
https://github.com/pennersr/django-allauth/issues/356#issuecomment-24758824
But it still does not work. The order doesn't change it all. sigh :(
--
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.
amit pant
2018-11-10 03:16:39 UTC
Permalink
can we use clean method instead of __init__
Post by Aashutosh Rathi
Yes, this works for me.
Make sure
super(AddLabForm, self).__init__(*args, **kwargs)
fields_keyOrder = ['id', 'date', 'start_time', 'end_time']
self.fields.keyOrder = fields_keyOrder
self.fields = OrderedDict((k, self.fields[k]) for k in
fields_keyOrder)
Post by Tobias Dacoir
It still complained about the request, but I was finally able to get it
super(CustomContactForm, self).__init__(request=request, *args,
**kwargs)
fields_keyOrder = ['name', 'reason', 'email', 'body']
self.fields.keyOrder = fields_keyOrder
self.fields = OrderedDict((k, self.fields[k]) for k in
fields_keyOrder)
However it still does not re-order the fields, I searched stackoverflow
https://github.com/pennersr/django-allauth/issues/356#issuecomment-24758824
But it still does not work. The order doesn't change it all. sigh :(
--
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
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
<https://groups.google.com/d/msgid/django-users/67309700-3cfc-4199-a789-8bc8653b1022%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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/CAF2Ah_HQqXkmMectBBUJkwJdYZLfbe8Qr6FBqQKwPxJ2Ln6MWg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...