{"id":765,"date":"2019-02-06T00:19:05","date_gmt":"2019-02-05T15:19:05","guid":{"rendered":"https:\/\/www.nblog09.com\/w\/?p=765"},"modified":"2021-11-11T17:35:54","modified_gmt":"2021-11-11T08:35:54","slug":"django-form-validation","status":"publish","type":"post","link":"https:\/\/www.nblog09.com\/w\/2019\/02\/06\/django-form-validation\/","title":{"rendered":"Django\u306e\u69d8\u3005\u306aForm\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3(django.core.validators, clean)\u306b\u3064\u3044\u3066\u307e\u3068\u3081\u307e\u3057\u305f"},"content":{"rendered":"<h2 class=\"title-border\"><strong><br \/>\nDjango\u306eForm\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u4f7f\u7528\u65b9\u6cd5<\/strong><br \/>\n<\/h2>\n<p>Django\u306eForm\u3067\u306f\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u6a5f\u80fd\u3092\u7528\u3044\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3068\u306f\u3001<span class=\"pink_span\">Form\u306e\u5024\u306e\u578b\u306a\u3069\u304c\u6b63\u3057\u3044\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3092\u3057\u3066<\/span>\u8aa4\u3063\u3066\u3044\u305f\u5834\u5408\u3001Form\u9001\u4fe1\u5f8c\u306e\u51e6\u7406\u306e\u5b9f\u884c\u3092\u4e2d\u6b62\u3059\u308b\u6a5f\u80fd\u306e\u3053\u3068\u3092\u8a00\u3046\u3002<\/p>\n<p>form.is_valid()\u3067\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<p>django.form.Form\u3092\u7d99\u627f\u3057\u305f\u30d5\u30a9\u30fc\u30e0\u306e\u30af\u30e9\u30b9\u5185\u3067\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u5b9a\u7fa9\u3059\u308b\u3002<\/p>\n<p>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5\u306b\u306f\u3001\u4ee5\u4e0b\u306e3\u3064\u306e\u65b9\u6cd5\u304c\u3042\u308b\u3002<\/p>\n<ol>\n<li><strong>django.core.validators\u3092\u30a4\u30f3\u30dd\u30fc\u30c8\u3057\u3066\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u4e2d\u306b\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3092\u6307\u5b9a\u3059\u308b\u3002<\/strong><\/li>\n<li><strong>clean_\u3068\u3044\u3046\u540d\u524d\u306e\u30e1\u30bd\u30c3\u30c9\u3092Form\u30af\u30e9\u30b9\u306e\u4e2d\u306b\u4f5c\u6210\u3059\u308b<\/strong><\/li>\n<li><strong>\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u7528\u306e\u95a2\u6570\u3092\u4f5c\u6210\u3057\u3066\u3001\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u4e2d\u3067\u547c\u3073\u51fa\u3059\u3088\u3046\u306b\u5b9a\u7fa9\u3059\u308b<\/strong><\/li>\n<\/ol>\n<p>\u4ee5\u4e0b\u3001\u305d\u308c\u305e\u308c\u306e\u4f7f\u3044\u65b9\u3092\u8a18\u8ff0\u3057\u305f\u3002<\/p>\n<div class=\"programming-gray\">\n<ul>\n<li>\u30d5\u30a9\u30fc\u30e0\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u4e2d\u306b\u6307\u5b9a\u3059\u308b\u65b9\u6cd5<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django import forms<br \/>\n<span class=\"codehighlight\">from django.core import validators<\/span> <strong># validator\u306e\u30a4\u30f3\u30dd\u30fc\u30c8<\/strong><\/p>\n<p>def check_for_z(value):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if value[0].lower() != 'z':<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise forms.ValidationError(\"NEEDS TO START WITH Z\")<\/p>\n<p>class FormName(forms.Form):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;name = forms.CharField()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;email = forms.EmailField()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;text = forms.CharField(widget=forms.Textarea)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;botcatcher = forms.CharField(required=False,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;widget=forms.HiddenInput,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"codehighlight\">validators=[validators.MaxLengthValidator(10)]<\/span>)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># validators=[validators.MaxLengthValidator(10)]\u3068\u3057\u3066<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># botcatcher\u306f\u6700\u5927\u306e\u9577\u3055\u306f10\u3068\u3057\u300210\u4ee5\u4e0a\u306e\u5024\u304c\u633f\u5165\u3055\u308c\u308b\u3068\u753b\u9762\u4e0a\u306b\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3001Form\u3092\u9001\u308b\u3053\u3068\u304c\u3067\u304d\u306a\u3044<\/strong><\/p>\n<\/div>\n<ul>\n<li>clean_\u30e1\u30bd\u30c3\u30c9\u3067\u5b9a\u7fa9\u3059\u308b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django import forms<br \/>\n<span class=\"codehighlight\">from django.core import validators<\/span><\/p>\n<p>class FormName(forms.Form):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;name = forms.CharField()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;email = forms.EmailField()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;verify_email = forms.EmailField(label='Enter your email again')<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;text = forms.CharField(widget=forms.Textarea)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;botcatcher = forms.CharField(required=False,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;widget=forms.HiddenInput,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validators=[validators.MaxLengthValidator(0)])<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"codehighlight\">def clean_botcatcher(self):<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># clean_botcatcher\u3068\u3057\u3066Form\u9001\u4fe1\u6642\u306bbotcatcher\u306e\u5024\u304c\u6b63\u3057\u3044\u304b\u78ba\u8a8d<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># \u4f8b\u306e\u5834\u5408\u3001botcatcher\u306e\u9577\u3055\u304c0\u4ee5\u4e0a\u306e\u5834\u5408\u306f\u30a8\u30e9\u30fc<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;botcatcher = self.cleaned_data['botcatcher']<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if len(botcatcher) &gt; 0:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise forms.ValidationError(\"GOTCHA BOT\")<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return botcatcher<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"codehighlight\">def clean(self):<\/span><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># clean()\u3067\u95a2\u6570\u3092\u5b9a\u7fa9\u3059\u308b\u3068\u3001Form\u9001\u4fe1\u6642\u306b\u3059\u3079\u3066\u306eform\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u30c1\u30a7\u30c3\u30af<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># \u4f8b\u3067\u306f\u3001email\u3068vmail\u304c\u6b63\u3057\u3044\u5834\u5408\u306f\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;all_clean_data = super().clean()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;email = all_clean_data['email']<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vmail = all_clean_data['verify_email']<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if email != vmail: <strong># email\u3068verify_email\u304c\u7b49\u3057\u304f\u306a\u3044\u5834\u5408ValidationError<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise forms.ValidationError(\"MAKE SURE EMAIL  MATCH\")<\/pre>\n<\/div>\n<ul>\n<li>\u95a2\u6570\u3092\u4f5c\u6210\u3057\u3066\u3001validators\u3067\u6307\u5b9a\u3059\u308b<\/li>\n<\/ul>\n<div class=\"programming-inside\">\nfrom django import forms<br \/>\nfrom django.core import validators<\/p>\n<p>def check_for_z(value): <strong># \u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u7528\u306b\u5b9a\u7fa9\u3057\u305f\u95a2\u6570<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;if value[0].lower() != 'z':<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise forms.ValidationError(\"NEEDS TO START WITH Z\")<strong> # \u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u30a8\u30e9\u30fc<\/strong><\/p>\n<p>class FormName(forms.Form):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;name = forms.CharField(<span class=\"codehighlight\">validators=[check_for_z]<\/span>)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># validators=[check_for_z]\u3068\u3057\u3066\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306bcheck_for_z\u95a2\u6570\u3092\u6307\u5b9a\u3059\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<strong># form\u9001\u4fe1\u6642\u306b\u3001check_for_z\u304c\u5b9f\u884c\u3055\u308c\u3001\u95a2\u6570\u306e\u5f15\u6570value\u306bname\u304c\u6e21\u3055\u308c\u308b<\/strong><br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;email = forms.EmailField()<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;text = forms.CharField(widget=forms.Textarea)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;botcatcher = forms.CharField(required=False,<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;widget=forms.HiddenInput)\n<\/div>\n<\/div>\n<div class=\"django-adsense\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Django\u306eForm\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u4f7f\u7528\u65b9\u6cd5 Django\u306eForm\u3067\u306f\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u6a5f\u80fd\u3092\u7528\u3044\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u3068\u306f\u3001Form\u306e\u5024\u306e\u578b\u306a\u3069\u304c\u6b63\u3057\u3044\u304b\u3069\u3046\u304b\u30c1\u30a7\u30c3\u30af\u3092\u3057\u3066\u8aa4\u3063\u3066\u3044\u305f\u5834\u5408\u3001Form\u9001\u4fe1\u5f8c\u306e [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[121,219],"tags":[],"class_list":{"0":"post-765","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-django","7":"category-it-programming","8":"entry"},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"home-middle":false,"home-top":false,"sidebar-thumbnail":false},"uagb_author_info":{"display_name":"9nmatsumoto","author_link":"https:\/\/www.nblog09.com\/w\/author\/9nmatsumoto\/"},"uagb_comment_info":4,"uagb_excerpt":"Django\u306eForm\u306e\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u306e\u4f7f\u7528\u65b9\u6cd5 Django\u306eForm\u3067\u306f\u3001\u30d0\u30ea\u30c7\u30fc\u30b7\u30e7\u30f3\u6a5f\u80fd\u3092\u7528\u3044\u308b\u3053\u3068\u304c&hellip;","_links":{"self":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/765","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/comments?post=765"}],"version-history":[{"count":15,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/765\/revisions"}],"predecessor-version":[{"id":3789,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/posts\/765\/revisions\/3789"}],"wp:attachment":[{"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/media?parent=765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/categories?post=765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nblog09.com\/w\/wp-json\/wp\/v2\/tags?post=765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}