0

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!

4

3 回答 3

1

恐怕你不能依赖 Django 的 POST dict 中字段的顺序——它本质上是一本字典,所以没有固定的顺序。

如果发布数据的顺序很重要,Djangorequest.raw_post_data会提供。

于 2013-06-30T19:53:28.537 回答
0

Python 字典是无序的。因为 Django 响应是一个字典,所以它的顺序是任意的,不能依赖。

于 2013-06-30T20:33:21.357 回答
-1

这是因为QueryDict是 dict 类型,所以它以任意顺序返回它的键。

最好将字典视为一组无序的键:值对

http://docs.python.org/3/tutorial/datastructures.html#dictionaries

于 2013-06-30T19:35:07.843 回答