ビュー.py
def add_phone(request):
user=request.user
try:
phone = Phone_info.objects.get(user=request.user)
except:
phone = None
save_msg = ''
form_error = False
phoneForm = PhoneForm({'user':request.user})
phoneForm = PhoneForm(instance=phone)
if request.method=='POST':
phoneForm = PhoneForm(request.POST,instance=phone)
if phoneForm.is_valid():
phone=phoneForm.save(commit=False)
phone.user = user
phone.save()
save_msg = "Your details have been saved"
return render_to_response('incident/add_phone.html',
{
'about_menu': True,
'PhoneForm' :phoneForm,
'save_msg':save_msg,
'form_error':form_error,
},
context_instance=RequestContext(request))
フォーム.py
class PhoneForm(forms.ModelForm):
emergency = forms.CharField(required=True)
emergency_number = forms.IntegerField(required=True)
class Meta:
model = Phone_info
fields = ['name1','number1','name2','number2','name3','number3','emergency','emergency_number']
def clean(self):
name1 = self.cleaned_data.get("name1", None)
number1 = self.cleaned_data.get("number1", None)
name2 = self.cleaned_data.get("name2", None)
number2 = self.cleaned_data.get("number2", None)
name3 = self.cleaned_data.get("name3", None)
number3 = self.cleaned_data.get("number3", None)
if name1 and not number1:
raise forms.ValidationError("This field is required")
if number1 and not name1:
raise forms.ValidationError("This field is required")
if name2 and not number2:
raise forms.ValidationError("This field is required")
if number2 and not name2:
raise forms.ValidationError("This field is required")
if name3 and not number3:
raise forms.ValidationError("This field is required")
if number3 and not name3:
raise forms.ValidationError("This field is required")
return self.cleaned_data
template.html
<table width="100%" cellpadding="0" cellspacing="0" id="phone">
<tr>
<td colspan="2"><h1 align="left">Call - default telephone numbers</h1> </td>
<tr>
<td>Person or area</td>
<td>Phone number</td>
</tr>
<tr>
<td>{{ PhoneForm.name1 }}{{ PhoneForm.name1.errors }}</td>
<td>{{ PhoneForm.number1 }}{{ PhoneForm.number1.errors }}</td>
</tr>
<tr>
<td>{{ PhoneForm.name2 }}{{ PhoneForm.name2.errors }}</td>
<td>{{ PhoneForm.number2 }}{{ PhoneForm.number2.errors }}</td>
</tr>
<tr>
<td>{{PhoneForm.name3}}{{ PhoneForm.name3.errors }}</td>
<td>{{PhoneForm.number3}}{{ PhoneForm.number3.errors }}</td>
</tr>
<tr>
<td>Emergency</td><td>Phone number</td>
</tr>
<tr>
<td>{{ PhoneForm.emergency }}{{ PhoneForm.emergency.errors }}</td>
<td>{{ PhoneForm.emergency_number }}{{ PhoneForm.emergency_number.errors }}</td>
</tr>
<tr><td colspan="2" align="right"> <p style=margin-top:2cm;>{% include "buttons/save.html" %}</p></td></tr>
</table></form>
フォーム検証の個々のエラー メッセージは表示されません。PhoneForm を使用しており、個々のフィールドについて、個々のエラー メッセージが表示されるはずです。確認しましたが、不足しているものが見つかりません。
ありがとう