我無法訪問通過我的模板提交的數據。這裏是我的代碼:Django訪問模板數據
模板:
<form action="" method="post">
{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
{{ form.as_p }}
{% endfor %}
<input type="submit" value="Submit">
</form>
查看:
def addMembers(request, id, members):
if request.user.is_authenticated():
members = int(members)
MemberFormSet = formset_factory(MemberForm, extra = members)
if request.method == 'POST':
print 'post'
formset = MemberFormSet(request.POST)
if formset.is_valid():
cd = formset.cleaned_data
for f in formset:
first_name = cd.first_name
last_name = cd.last_name
email = cd.email
house = House.objects.get(id = id)
member = Member(first_name = first_name, last_name = last_name, email = email, house = house, created_on = timezone.now())
member.save()
return HttpResponseRedirect(reverse('houses:controlPanel'))
else:
formset = MemberFormSet()
return render_to_response('houses/add_members.html', {'formset' : formset}, context_instance = RequestContext(request))
return HttpResponseRedirect(reverse('allauth.account.views.login'))
我試圖訪問數據一旁的幾種方法什麼是目前寫的,包括: f['attribute']
cd['attribute']
request.POST['attribute']
formset.cleaned_data['attribute']
我得到的錯誤範圍從u"Key 'email' not found in Form"
到list indices must be integers, not str
我該如何訪問由formset提交的數據? Django的文檔沒有給出明確的答案,並且四處搜索我發現了很多不同的方法,但都沒有工作。謝謝你的幫助。
你不需要檢查'f.is_valid()'。除非所有表單都有效,否則表單無效。 –
@PeterDeGlopper你是對的 – karthikr
任何方式來檢查之前is_valid? – radtek