[Django]バリデータ関数の作成とエラーメッセージ

### バリデータ作成
– reがpythonの正規表現モジュール
/hello/models.py

import re
from django.db import models
from django.core.validators import ValidationError

def number_only(value):
	if(re.match(r'^[0-9]*$', value) == None):
		raise ValidationError(
				'%(value)s is not Number!', params={'value': value},
			)

class Friend(models.Model):
	name = models.CharField(max_length=100, validators=[number_only])
	mail = models.EmailField(max_length=200)
	gender = models.BooleanField()
	age = models.IntegerField()
	birthday = models.DateField()

### エラーメッセージ作成
– フォーム全体のエラーメッセージを取り出すにはform.errorsを使う
– フォームの項目からエラーを取り出すことができる
/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<ol class="list-group">
		{% for item in form %}
		<li class="list-group-item py-2">{{ item.name }}{{ item.value }}:{{ item.errors.as_text }}</li>
		{% endfor %}
	</ol>
	<table class="table mt-4">
		<form action="{% url 'check' %}" method="post">
			{% csrf_token %}
			<tr>
				<th>名前</th>
				<td>{{ form.name }}</td>
			</tr>
			<tr>
				<th>メール</th>
				<td>{{ form.mail }}</td>
			</tr>
			<tr>
				<th>性別</th>
				<td>{{ form.gender }}</td>
			</tr>
			<tr>
				<th>年齢</th>
				<td>{{ form.age }}</td>
			</tr>
			<tr>
				<th>誕生日</th>
				<td>{{ form.birthday }}</td>
			</tr>
			<tr><th></th><td>
			<input type="submit" value="click" class="btn btn-primary">
			</td></tr>
		</form>
	</table>
</body>

### ModelFormのカスタマイズ
/hello/forms.py

class FriendForm(forms.ModelForm):
	class Meta:
		model = Friend
		fields = ['name','mail','gender','age','birthday']
		widgets = {
			'name': forms.TextInput(attrs={'class':'form-control'}),
			'mail': forms.EmailInput(attrs={'class':'form-control'}),
			'age': forms.NumberInput(attrs={'class':'form-control'}),
			'birthday': forms.DateInput(attrs={'class':'form-control'}),
		}

/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<ol class="list-group mb-4">
		{% for item in form %}
		<li class="list-group-item py-2">{{ item.name }}{{ item.value }}:{{ item.errors.as_text }}</li>
		{% endfor %}
	</ol>
		<form action="{% url 'check' %}" method="post">
			{% csrf_token %}
			<div class="form-group">名前{{ form.name }}</div>
			<div class="form-group">メール{{ form.mail }}</div>
			<div class="form-group">性別{{ form.gender }}</div>
			<div class="form-group">年齢{{ form.age }}</div>
			<div class="form-group">誕生日{{ form.birthday }}</div>
			<div class="form-group">
			<input type="submit" value="click" class="btn btn-primary">
			</div>
</body>

[Django]ModelFormのバリデーション

– バリデーションはsaveの時に実行されるので、モデルの更新時にはモデル側でバリデーションをかける

/hello/forms.py

class FriendForm(forms.ModelForm):
	class Meta:
		model = Friend
		fields = ['name','mail','gender','age','birthday']

/hello/models.py

class Friend(models.Model):
	name = models.CharField(max_length=100)
	mail = models.EmailField(max_length=200)
	gender = models.BooleanField()
	age = models.IntegerField(default=0)
	birthday = models.DateField()

– save以外に自分でチェックをかけることも可能
/hello/views.py

def check(request):
	params = {
		'title': 'Hello',
		'message': 'check validation.',
		'form': FriendForm(),
	}
	if(request.method == 'POST'):
		obj = Friend()
		form = FriendForm(request.POST, instance=obj)
		params['form'] = form
		if(form.is_valid()):
			params['message'] = 'OK!'
		else:
			params['message'] = 'no good.'
	return render(request, 'hello/check.html', params)

/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<form action="{% url 'check' %}" method="post">
		{% csrf_token %}
		<table class="table">
		{{ form.as_table }}
		<tr><th></th><td>
		<input type="submit" value="click" class="btn btn-primary mt-2">
		</td></tr>
		</table>
	</form>
</body>

– forms.FormとModelFormのバリデーションは異なる

### モデルにバリデーションの組み込み
/hello/models.py

from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator

class Friend(models.Model):
	name = models.CharField(max_length=100)
	mail = models.EmailField(max_length=200)
	gender = models.BooleanField()
	age = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(150)])
	birthday = models.DateField()

### モデルで使えるバリデータ
– MinValueValidator/MaxValueValidator, MinLengthValidator/MaxLengthValidator

from django.db import models
from django.core.validators import MinLengthValidator

class Friend(models.Model):
	name = models.CharField(max_length=100, validators=[MinLengthValidator(10)])
	mail = models.EmailField(max_length=200, validators=[MinLengthValidator(10)])
	gender = models.BooleanField()
	age = models.IntegerField()
	birthday = models.DateField()

– EmailValidator/URLValidator
– ProhibitNullCharactersValidator
– RegexValidator

from django.core.validators import RegexValidator

class Friend(models.Model):
	name = models.CharField(max_length=100, validators=[RegexValidator(r'^[a-z]*$')])

バリデーション定義によってmodelに組み込んでいくイメージです。

[Django]バリデーションの種類

### CharFieldのバリデーション
– required
– min_length, max_length
– empty_value

class CheckForm(forms.Form):
	empty = forms.CharField(label='Empty', empty_value=True, widget=forms.TextInput(attrs={'class':'form-control'}))
	min = forms.CharField(label='Min', min_length=10, widget=forms.TextInput(attrs={'class':'form-control'}))	
	max = forms.CharField(label='Max', max_length=10, widget=forms.TextInput(attrs={'class':'form-control'}))

### IntegerField/FloatFieldのバリデーション
– required
– min_value, max_value

class CheckForm(forms.Form):
	required = forms.IntegerField(label='Required', widget=forms.NumberInput(attrs={'class':'form-control'}))
	min = forms.IntegerField(label='Min', min_value=100, widget=forms.NumberInput(attrs={'class':'form-control'}))	
	max = forms.IntegerField(label='Max', max_value=1000, widget=forms.NumberInput(attrs={'class':'form-control'}))

### 日時関連のバリデーション
– DateField, TimeField, DateTimeFieldでは日時の各値を表す記号を組み合わせて作成する
– %y, %m, %d, %H, %M, %S

class CheckForm(forms.Form):
	date = forms.DateField(label='Date', input_formats=['%d'], widget=forms.DateInput(attrs={'class':'form-control'}))
	time = forms.TimeField(label='Time', widget=forms.TimeInput(attrs={'class':'form-control'}))
	datetime = forms.DateTimeField(label='DateTime', widget=forms.DateTimeInput(attrs={'class':'form-control'}))

### バリデーションを追加
– Formクラスにメソッドを追加する
– raise ValidationError(“Error message”)
/hello/forms.py

from django import forms

class CheckForm(forms.Form):
	str = forms.CharField(label='String', widget=forms.TextInput(attrs={'class':'form-control'}))

	def clean(self):
		cleaned_data = super().clean()
		str = cleaned_data['str']
		if(str.lower().startswith('no')):
			raise forms.ValidationError('Your input "No!"')

[Django]バリデーション基礎

if(<

>.is_valid()):
# エラー時の処理
else:
# 正常時の処理

### バリデーションを使ってみる
/hello/templates/hello/check.html

<body class="container">
	<h1 class="display-4 text-primary">{{title}}</h1>
	<p>{{message|safe}}</p>
	<form action="{% url 'check' %}" method="post">
		{% csrf_token %}
		{{ form.as_table }}
		<input type="submit" value="click" class="btn btn-primary mt-2">
	</form>
</body>

/hello/urls.py

	path('check', views.check, name='check')

/hello/forms.py

class CheckForm(forms.Form):
	str = forms.CharField(label='Name', widget=forms.TextInput(attrs={'class':'form-control'}))	

/hello/views.py

from .forms import CheckForm

def check(request):
	params = {
		'title': 'Hello',
		'message': 'check validation.',
		'form': CheckForm(),
	}
	if(request.method == 'POST'):
		form = CheckForm(request.POST)
		param['form'] = form
		if(form.is_valid()):
			params['message'] = 'OK!'
		else:
			params['message'] = 'no good.'
	return render(request, 'hello/check.html', params)

スペルミスでmethod=”post”がmetho=”post”になってた。道理で動かない訳だ。