1
我想在提交表單後保留表單字段。我可以保留,除了選中的複選框中的所有字段在django中發帖後保留複選框
這裏是我的表單操作視圖:
def student_submit_info(request):
form_values = {}
if request.method == 'POST':
form = StudentForm(request.POST)
print form.is_valid()
form_values = getDetails(request)
if form.is_valid():
print request.user.username
student = Student.objects.get(user=request.user)
// saving student to DB
saveDetails(request, student, form_values)
return render_to_response('student.html', form_values, context_instance=RequestContext(request))
def getDetails(request):
#fetch input data
form_values = {}
form_values['first_name'] = request.POST['first_name']
form_values['last_name'] = request.POST['last_name']
form_values['skills'] = request.POST.getlist('skills[]')
return form_values
我會希望保留技能(複選框),這裏是我的模板的一部分
skill1<input type="checkbox" name="skills[]" checked value="skill1"/>
skill2<input type="checkbox" name="skills[]" value="skill2"/>
skill3<input type="checkbox" name="skills[]" value="skill3"/>
爲什麼使用'user = User.objects.get(username = request.user.username)'而不是簡單的request.user? – Zulu 2014-09-04 15:57:05
你的'form'變量來自哪裏? – Zulu 2014-09-04 16:00:44
我不認爲Django表單支持'name ='something []「'表示法。這可能會更簡單的使用內置的小部件,看到這個答案:http://stackoverflow.com/questions/4381300/django-forms-list-of-checkboxes-list-of-radiobuttons – frnhr 2014-09-04 19:25:17