I am trying to create a form using django form library, yet when viewing the model populated by the form, the values are out of order..for no apparent reason.
Here is my view:
def reoccurring_view(request):
if request.method == 'POST':
form = ReoccurringForm(request.POST)
counter = 0
if form.is_valid():
for key, value in request.POST.iteritems():
counter += 1
if value is not None:
day = itemize(value, counter)
add = Reoccurring(day.Day, day.N, day.S, day.E)
add.save()
else:
form = ReoccurringForm()
return render(request, 'Reoccurring.html', {'form': form})
here is my template:
<form action="" method="post">
<table>
{{ form.as_table }}
</table>
{% csrf_token %}
<input class="btn btn-primary" style="float: left;" type="submit" value="Submit">
</form>
here is the resulting html form (note that it is in order):
Monday:
Tuesday:
Wednesday:
Thursday:
Friday:
Saturday:
Sunday:
here is my form class:
class ReoccurringForm(forms.Form):
monday = forms.CharField(required=False)
tuesday = forms.CharField(required=False)
wednesday = forms.CharField(required=False)
thursday = forms.CharField(required=False)
friday = forms.CharField(required=False)
saturday = forms.CharField(required=False)
sunday = forms.CharField(required=False)
Yet here are is the resulting populated model via admin:
1 [u'monday'] [u'06:00 p.m.'] [u'07:30 p.m.']
2 [u'tuesday'] [u'06:00 p.m.'] [u'07:30 p.m.']
3 [u'friday'] [u'06:00 p.m.'] [u'07:30 p.m.']
4 [u'wednesday'] [u'08:30 a.m.'] [u'09:30 a.m.']
5 [u'thursday'] [u'06:00 p.m.'] [u'07:30 p.m.']
6 [u'sunday'] [u'06:00 p.m.'] [u'07:30 p.m.']
7 [] [] []
8 [u'saturday'] [u'06:00 p.m.'] [u'07:30 p.m.']
as you can see...they are out of order, along with an extra position that should not be there..is this a bug?(EDIT: the csrf token is passed into the dict as well, easily ignored) But the ordering is still a mystery!
Thanks!
EDIT: Upon further investigation I decided to output the dict itself and see if it was broken and it was, no idea why though ):
> <QueryDict: {u'monday': [u'monday, 06:00 p.m. to 07:30 p.m.'],
> u'tuesday': [u'tuesday, 06:00 p.m. to 07:30 p.m.'], u'friday':
> [u'friday, 06:00 p.m. to 07:30 p.m.'], u'wednesday': [u''],
> u'thursday': [u'thursday, 06:00 p.m. to 07:30 p.m.'], u'sunday':
> [u'sunday, 06:00 p.m. to 07:30 p.m.'], u'csrfmiddlewaretoken':
> [u'AcxRjdNeTFwij7vwtdplZPy2SRlwrnzl'], u'saturday': [u'saturday, 06:00
> p.m. to 07:30 p.m.']}>
I even tried to explicitly set the ordering of the fields:
def __init__(self, *args, **kwargs):
super(ReoccurringForm, self).__init__(*args, **kwargs)
self.fields.keyOrder = [
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday']
But this makes no difference whatsoever...it seems the ordering is correct, but the processing of the data into the POST dict is somehow getting messed up, any insight would be greatly appreciated!