March 11, 2010
Django:フォームのバリデーション

フォームのフィールド名の先頭に “clean_”をつけたメソッドを定義します。


# -*- coding: utf-8 -*-
from django import forms
from django.contrib.formtools.preview import FormPreview
from django.http import HttpResponseRedirect
import re
class MessageForm(forms.Form):
    message = forms.CharField()     def clean_message(self):
        _c_message = self.cleaned_data[‘message’]
        if re.match(“^\d+$”,_c_message) == None:
            raise forms.ValidationError(u’数字を入力してください’)
        else:
            return _c_message

       

アルファベットでPreviewすると、エラー


数字でPreviewだとOK

      

Posted via email from 原宿工業大学 | Comment »