... >> f . is_valid () True Let's try with some invalid data. In this case, subject is blank (an error, because all fields are required by default) and contact_email is not a valid email address: >>> data = { ... "subject" : "" , ... "message" : "Hi there" , ... Form. errors ¶ errors 属性にアクセスして、エラーメッセージの辞書を取得します。 >>> f . errors {'subject': ['This field is required.'], 'contact_email': ['Enter a valid email address.']} この辞書では、キーはフィールド名であり、値はエラーメッセージを表す文 ... or インスタンスをマッピングする dict を返します。 >>> f . errors . as_data () {'subject': [ValidationError(['This field is required.'])], 'contact_email': [ValidationError(['Enter a valid email address.'])]} Use this method anytime you need to identify a ... ... >> f . is_valid () True Let's try with some invalid data. In this case, subject is blank (an error, because all fields are required by default) and contact_email is not a valid email address: >>> data = { ... "subject" : "" , ... "message" : "Hi there" , ... Form. errors ¶ errors 属性にアクセスして、エラーメッセージの辞書を取得します。 >>> f . errors {'subject': ['This field is required.'], 'contact_email': ['Enter a valid email address.']} この辞書では、キーはフィールド名であり、値はエラーメッセージを表す文 ... or インスタンスをマッピングする dict を返します。 >>> f . errors . as_data () {'subject': [ValidationError(['This field is required.'])], 'contact_email': [ValidationError(['Enter a valid email address.'])]} Use this method anytime you need to identify a ... ... 。 Field クラスによっては追加のフィールド固有の引数も取れることがありますが、以下に説明する引数は 常に 取ることができます。 required ¶ Field. required ¶ デフォルトでは、各 Field クラスは値が必須であると想定しているため、空の値 ( None もしくは空文字列 ( "" ... >> f . clean ( "foo" ) 'foo' >>> f . clean ( "" ) Traceback (most recent call last): ... ValidationError : ['This field is required.'] >>> f . clean ( None ) Traceback (most recent call last): ... ValidationError : ['This field is required.'] >>> f . cle ... clean ( True ) 'True' >>> f . clean ( False ) 'False' フィールドが必須で ない ことを指定するには、 Field コンストラクタに required=False を渡します: >>> f = forms . CharField ( required = False ) >>> f . clean ( "foo" ) 'foo' >>> f . clean ( "" ) '' >>> ... ... 。 Field クラスによっては追加のフィールド固有の引数も取れることがありますが、以下に説明する引数は 常に 取ることができます。 required ¶ Field. required ¶ デフォルトでは、各 Field クラスは値が必須であると想定しているため、空の値 ( None もしくは空文字列 ( "" ... >> f . clean ( "foo" ) 'foo' >>> f . clean ( "" ) Traceback (most recent call last): ... ValidationError : ['This field is required.'] >>> f . clean ( None ) Traceback (most recent call last): ... ValidationError : ['This field is required.'] >>> f . cle ... clean ( True ) 'True' >>> f . clean ( False ) 'False' フィールドが必須で ない ことを指定するには、 Field コンストラクタに required=False を渡します: >>> f = forms . CharField ( required = False ) >>> f . clean ( "foo" ) 'foo' >>> f . clean ( "" ) '' >>> ... ... w ( request ): if not request . user . is_authenticated : return render ( request , "myapp/login_error.html" ) # ... login_required デコレータ ¶ login_required ( redirect_field_name = 'next' , login_url = None ) [ソース] ¶ ショートカットとして、便利な lo ... gin_required() デコレータを利用できます: from django.contrib.auth.decorators import login_required @login_required def my_view ( reques ... t ): ... login_required() は下記の処理を行います: もしユーザがログインしていなければ、 settings.LOGIN_URL にリダイレクトし、クエリ文字列に現在の絶 ... ... w ( request ): if not request . user . is_authenticated : return render ( request , "myapp/login_error.html" ) # ... login_required デコレータ ¶ login_required ( redirect_field_name = 'next' , login_url = None ) [ソース] ¶ ショートカットとして、便利な lo ... gin_required() デコレータを利用できます: from django.contrib.auth.decorators import login_required @login_required def my_view ( reques ... t ): ... login_required() は下記の処理を行います: もしユーザがログインしていなければ、 settings.LOGIN_URL にリダイレクトし、クエリ文字列に現在の絶 ... ... Field ( widget = forms . SelectDateWidget ( years = BIRTH_YEAR_CHOICES ) ) favorite_colors = forms . MultipleChoiceField ( required = False , widget = forms . CheckboxSelectMultiple , choices = FAVORITE_COLORS_CHOICES , ) 使用可能なウィジェットとそれぞれ ... や追加の属性はありません。 >>> f = CommentForm ( auto_id = False ) >>> print ( f ) <div>Name:<input type="text" name="name" required></div> <div>Url:<input type="url" name="url" required></div> <div>Comment:<input type="text" name="comment" required></div ... になります: >>> f = CommentForm ( auto_id = False ) >>> print ( f ) <div>Name:<input type="text" name="name" class="special" required></div> <div>Url:<input type="url" name="url" required></div> <div>Comment:<input type="text" name="comment" size="40" requ ... ... のそれぞれの値が保存されます。 True に設定すると、末尾の値が空白の場合、その値は結果から取り除かれます。基となるフィールドが required=True で、かつ remove_trailing_nulls が True の場合、末尾にのみ null 値が許され、その値は取り除かれます。 例: SplitArrayFie ... ld ( IntegerField ( required = True ), size = 3 , remove_trailing_nulls = False ) [ "1" , "2" , "3" ] # -> [1, 2, 3] [ "1" , "2" , "" ] # -> Validation ... Error - third entry required. [ "1" , "" , "3" ] # -> ValidationError - second entry required. [ "" , "2" , "" ] # -> ValidationError - first and third ... ... Field ( widget = forms . SelectDateWidget ( years = BIRTH_YEAR_CHOICES ) ) favorite_colors = forms . MultipleChoiceField ( required = False , widget = forms . CheckboxSelectMultiple , choices = FAVORITE_COLORS_CHOICES , ) 使用可能なウィジェットとそれぞれ ... や追加の属性はありません。 >>> f = CommentForm ( auto_id = False ) >>> print ( f ) <div>Name:<input type="text" name="name" required></div> <div>Url:<input type="url" name="url" required></div> <div>Comment:<input type="text" name="comment" required></div ... になります: >>> f = CommentForm ( auto_id = False ) >>> print ( f ) <div>Name:<input type="text" name="name" class="special" required></div> <div>Url:<input type="url" name="url" required></div> <div>Comment:<input type="text" name="comment" size="40" requ ... ... のそれぞれの値が保存されます。 True に設定すると、末尾の値が空白の場合、その値は結果から取り除かれます。基となるフィールドが required=True で、かつ remove_trailing_nulls が True の場合、末尾にのみ null 値が許され、その値は取り除かれます。 例: SplitArrayFie ... ld ( IntegerField ( required = True ), size = 3 , remove_trailing_nulls = False ) [ "1" , "2" , "3" ] # -> [1, 2, 3] [ "1" , "2" , "" ] # -> Validation ... Error - third entry required. [ "1" , "" , "3" ] # -> ValidationError - second entry required. [ "" , "2" , "" ] # -> ValidationError - first and third ... 2026年10月08日