注册表包含多个ContactForm和FeeForm,我尝试验证FeeForm,注册表中存在的ContactForms,验证失败并且没有显示任何验证错误,对于空的FeeForm字段,联系表单
forms.py
class ContactForm(forms.Form):
address = forms.CharField(widget = forms.Textarea(attrs = {'rows' : '5'}),required = False)
phone = forms.CharField(required = True)
email = forms.EmailField(required = False)
class FeeForm(forms.Form):
name_of_bank = forms.CharField(required = True)
dd_number = forms.CharField(required = True)
date= forms.DateField(required = True)
amount = forms.CharField(required=True)
def clean_amount(self):
amount = self.cleaned_data['amount']
if amount == '':
self._errors["amount"] = ["Please Enter Amount"]
return amount
class RegistrationForm(forms.Form):
hall_ticket_number = forms.CharField(required = False)
name = forms.CharField(required=True)
religion = forms.CharField(required = False)
present_contact = ContactForm()
gender = forms.ChoiceField( choices = ( ('M', 'Male'), ('F', 'Female')),required = True)
organization_contact = ContactForm()
fee_details = FeeForm()
def clean_hall_ticket_number(self):
hall = self.cleaned_data['hall_ticket_number']
if hall == '':
self._errors["hall_ticket_number"] = ["Hall Ticket Number is NOt Present"]
return hall
def clean_fee_detaills(self):
amount = self.cleaned_data['amount']
if amount == "":
self._errors["hall_ticket_number"] = ["Hall Ticket Number is NOt Present"]
self._errors["amount"] = ["Hall Ticket Number is NOt Present"]
return amount
'''def clean(self):
clean_data=super(RegistrationForm, self).clean()
amount = self.cleaned_data['amount']
if amount == "":
self._errors["amount"] = ["Please Enter Amount"]
return amount
'''
Views.py
initial_values = {
'name' : student.name,
'hall_ticket_number' : student.hall_ticket_no,
'gender' : student.sex,
'religion' : student.religion,
'present_contact' : {
'address' : student.present_contact.address,
'phone' : student.present_contact.phone_no,
'email' : student.present_contact.email,
},
'organization_contact' : {
'address' : student.original_contact.address,
'phone' : student.original_contact.phone_no,
'email' : student.original_contact.email,
},
}
registration_status = False
if request.method == 'POST':
form = RegistrationForm(request.POST)
#Checking Form is Valid or not
if form.is_valid():
text = register(request,student_id) # Processing of Register Page in registartion_contrib
return redirect('/registration/{student_id}/register'.format(**locals()))
else:
registrations = Registration.objects.filter(student = student)
f_initial_values = None
# Checking Registration Status
if len(registrations) > 0:
registration = registrations[0]
if registration.status == 'Done':
registration_status = True
# Feedetails of Student
feedetails = FeeDetail.objects.filter(student = student)
if len(feedetails) > 0:
feedetail = feedetails[0]
# Initializing the FeeDetails for Form
f_initial_values = {}
f_initial_values['name_of_bank'] = feedetail.name_of_bank
f_initial_values['dd_number'] = feedetail.dd_number
f_initial_values['amount'] = feedetail.amount
f_initial_values['date'] =feedetail.fee_date.strftime("%Y-%m-%d")
# Specifing the initial values for Forms
form = RegistrationForm(initial = initial_values, )
if f_initial_values != None:
form.fee_details = FeeForm(initial = f_initial_values)
# Initializing Contact Forms PRESENT, Organization, Supervisor and Co Supervisor
form.present_contact = ContactForm(initial = initial_values['present_contact'], prefix="present_contact")
form.organization_contact = ContactForm(initial = initial_values['organization_contact'], prefix="organization_contact")
form.supervisor_contact = ContactForm( initial = {
"address" : student.guide.contact.address,
"phone" : student.guide.contact.phone_no,
"email" : student.guide.contact.email,
}, prefix = "supervisor_contact" )
form.co_supervisor_contact = ContactForm( initial = {
"address" : student.co_guide.contact.address,
"phone" : student.co_guide.contact.phone_no,
"email" : student.co_guide.contact.email,
}, prefix = "co_supervisor_contact" )
return render(request, 'registration/register.html', {'student' : student, 'form' : form, 'registration_status' : registration_status,'registration':registration })
template.html
Forms fields are specified as follows
{{ form.hall_ticket_number|materializecss}}
{{ form.name|materializecss}}
{{ form.religion|materializecss}}
{{ form.gender|materializecss}}
{{ form.orgnization_contact.address}}
{{ form.orgnization_contact.phone}}
{{ form.orgnization_contact.email}}
{{ form.present_contact.address}}
{{ form.present_contact.phone}}
{{ form.present_contact.email}}
<div class="col s12 m12 l12">
{{ form.fee_details.name_of_bank|materializecss }}
</div>
<div class="col s12 m12 l12">
{{ form.fee_details.dd_number.errors }}
{{ form.fee_details.dd_number|materializecss }}
</div>
<div class="col s12 m12 l12">
{{ form.fee_details.date.errors }}
{{ form.fee_details.date|materializecss}}
</div>
<div class="col s12 m12 l12">
{{ form.errors }}
<br>{{ form.fee_details.amount|materializecss }}
</div>
</div>
答案 0 :(得分:1)
每个单独的表单都需要调用自己的form.is_valid()
来获取模板中的错误字段。在您的post方法中,只有一个form.is_valid(
}被调用,仅用于RegistrationForm
。更独立地发送/接收三个表单。在模板中,在一个表单下使用它们。
事情可以通过这种方式完成:
views.py
if request.method == 'POST':
reg_form = RegistrationForm(request.POST)
contact_form = ContactForm(request.POST)
free_form = FeeForm(request.POST)
reg_valid = reg_form.is_valid()
contact_valid = contact_form.is_valid()
free_valid = free_form .is_valid()
if reg_valid and contact_valid and free_valid:
#Everything is fine, do the registration and go to success page
else:
#error happened, so go to form page with error fields
return render(request, 'registration/register.html', {'student' : student, 'reg_form' : reg_form,'contact_form' : contact_form, 'free_form' : free_form,'registration_status' : registration_status,'registration':registration })
else:
reg_form = RegistrationForm(initials=...)
contact_form = ContactForm(initials=...)
free_form = FeeForm(initials=...)
return render(request, 'registration/register.html', {'student' : student, 'reg_form' : reg_form,'contact_form' : contact_form, 'free_form' : free_form,'registration_status' : registration_status,'registration':registration })