我想創建一個使用django表單庫的表單,但當查看錶單填充的模型時,值是無序的..沒有明顯的原因。Django表單訂購是歪
這是我的觀點:
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})
這裏是我的模板:
<form action="" method="post">
<table>
{{ form.as_table }}
</table>
{% csrf_token %}
<input class="btn btn-primary" style="float: left;" type="submit" value="Submit">
</form>
這裏是生成的HTML表格(注意,是按順序):
Monday:
Tuesday:
Wednesday:
Thursday:
Friday:
Saturday:
Sunday:
這裏是我的表格課程:
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)
然而,這裏都是通過管理產生的人口模型:
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.']
你可以看到......他們都失靈,一個額外的立場,即不應該是there..is此沿着bug?(編輯:csrf令牌也被傳入字典中,很容易忽略)但是排序仍然是一個謎!
謝謝!
編輯:經進一步調查,我決定輸出字典本身,看看它是否被打破,這是,不知道爲什麼雖然):
> <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.']}>
我甚至嘗試明確設置字段的順序:
def __init__(self, *args, **kwargs):
super(ReoccurringForm, self).__init__(*args, **kwargs)
self.fields.keyOrder = [
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday']
但是這使得沒有任何區別...似乎排序是正確的,但數據進入後字典的處理以某種方式得到搞砸了,任何有識之士將不勝感激!
而這種順序是糟糕的時候表單是RENDERED或只是你傾銷該QueryDict? – Krab
呈現的表單在原始問題中一直是正確的,我指定並注意到呈現的順序是正確的。只是POST轉儲不正確。 – ljrh