... できます。 class Form [ソース] ¶ バインドされていない Form のインスタンスを作成するには、クラスの初期化を行います。 >>> f = ContactForm () フォームにデータをバインドするには、辞書型のデータを Form クラスのコンストラクタの最初のパラメータとして渡します。 >>> ... ct" : "hello" , ... "message" : "Hi there" , ... "contact_email" : "foo@example.com" , ... "urgent" : True , ... } >>> f = ContactForm ( data ) この辞書では、キーは Form クラスの属性に対応するフィールド名を、値には検証したいデータを指定します。これらは通常文 ... ンスと、されていないフォームインスタンスを区別する必要があるときは、フォームの is_bound 属性の値を確認してください。 >>> f = ContactForm () >>> f . is_bound False >>> f = ContactForm ({ "subject" : "hello" }) >>> f . is_bound True 空の辞書を渡すと、データが空 ... ... できます。 class Form [ソース] ¶ バインドされていない Form のインスタンスを作成するには、クラスの初期化を行います。 >>> f = ContactForm () フォームにデータをバインドするには、辞書型のデータを Form クラスのコンストラクタの最初のパラメータとして渡します。 >>> ... ct" : "hello" , ... "message" : "Hi there" , ... "contact_email" : "foo@example.com" , ... "urgent" : True , ... } >>> f = ContactForm ( data ) この辞書では、キーは Form クラスの属性に対応するフィールド名を、値には検証したいデータを指定します。これらは通常文 ... ンスと、されていないフォームインスタンスを区別する必要があるときは、フォームの is_bound 属性の値を確認してください。 >>> f = ContactForm () >>> f . is_bound False >>> f = ContactForm ({ "subject" : "hello" }) >>> f . is_bound True 空の辞書を渡すと、データが空 ... ... 。これはこのタイプのフィールドに特有であり、それはこの後の使われ方には関係ありません。 このフィールドの使い方を示すために、 ContactForm を作成してみましょう: class ContactForm ( forms . Form ): subject = forms . CharField ( max_length = 100 ) message = forms ... ソッドを呼び出します。 特定のフィールド属性をクリーニングする ¶ Continuing on from the previous example, suppose that in our ContactForm , we want to make sure that the recipients field only contains addresses that are @example.com . This is validation that i ... erates on the recipients field, like so: from django import forms from django.core.exceptions import ValidationError class ContactForm ( forms . Form ): # Everything as before. ... def clean_recipients ( self ): data = self . cleaned_data [ "recipients" ] i ... ... 。これはこのタイプのフィールドに特有であり、それはこの後の使われ方には関係ありません。 このフィールドの使い方を示すために、 ContactForm を作成してみましょう: class ContactForm ( forms . Form ): subject = forms . CharField ( max_length = 100 ) message = forms ... ソッドを呼び出します。 特定のフィールド属性をクリーニングする ¶ Continuing on from the previous example, suppose that in our ContactForm , we want to make sure that the recipients field only contains addresses that are @example.com . This is validation that i ... erates on the recipients field, like so: from django import forms from django.core.exceptions import ValidationError class ContactForm ( forms . Form ): # Everything as before. ... def clean_recipients ( self ): data = self . cleaned_data [ "recipients" ] i ... ... 意しています。 基本的なフォーム ¶ 以下のようなコンタクトフォームがあったとします。 forms.py ¶ from django import forms class ContactForm ( forms . Form ): name = forms . CharField () message = forms . CharField ( widget = forms . Textarea ) def send_email ( s ... elf.cleaned_data dictionary pass このとき、ビューは FormView を使うことで構築できます。 views.py ¶ from myapp.forms import ContactForm from django.views.generic.edit import FormView class ContactFormView ( FormView ): template_name = "contact.html" form_cla ... ss = ContactForm success_url = "/thanks/" def form_valid ( self , form ): # This method is called when valid form data has been POSTed. # I ... ... jango.views.generic.edit.ProcessFormView django.views.generic.base.View 例 myapp/forms.py : from django import forms class ContactForm ( forms . Form ): name = forms . CharField () message = forms . CharField ( widget = forms . Textarea ) def send_email ( s ... elf ): # send email using the self.cleaned_data dictionary pass 例 myapp/views.py : from myapp.forms import ContactForm from django.views.generic.edit import FormView class ContactFormView ( FormView ): template_name = "contact.html" form_cla ... ss = ContactForm success_url = "/thanks/" def form_valid ( self , form ): # This method is called when valid form data has been POSTed. # I ... ... 意しています。 基本的なフォーム ¶ 以下のようなコンタクトフォームがあったとします。 forms.py ¶ from django import forms class ContactForm ( forms . Form ): name = forms . CharField () message = forms . CharField ( widget = forms . Textarea ) def send_email ( s ... elf.cleaned_data dictionary pass このとき、ビューは FormView を使うことで構築できます。 views.py ¶ from myapp.forms import ContactForm from django.views.generic.edit import FormView class ContactFormView ( FormView ): template_name = "contact.html" form_cla ... ss = ContactForm success_url = "/thanks/" def form_valid ( self , form ): # This method is called when valid form data has been POSTed. # I ... ... ォルト値は、フォームの一部であるすべてのウィジェットの media 定義を追加した結果です: >>> from django import forms >>> class ContactForm ( forms . Form ): ... date = DateField ( widget = CalendarWidget ) ... name = CharField ( max_length = 40 , widget = Other ... Widget ) ... >>> f = ContactForm () >>> f . media <link href="https://static.example.com/pretty.css" media="all" rel="stylesheet"> <script src="https://sta ... フォームレイアウト用のCSSなど、フォームに追加のアセットを関連付けたい場合は、フォームに Media 宣言を追加します: >>> class ContactForm ( forms . Form ): ... date = DateField ( widget = CalendarWidget ) ... name = CharField ( max_length = 40 , widget = Other ... ... jango.views.generic.edit.ProcessFormView django.views.generic.base.View 例 myapp/forms.py : from django import forms class ContactForm ( forms . Form ): name = forms . CharField () message = forms . CharField ( widget = forms . Textarea ) def send_email ( s ... elf ): # send email using the self.cleaned_data dictionary pass 例 myapp/views.py : from myapp.forms import ContactForm from django.views.generic.edit import FormView class ContactFormView ( FormView ): template_name = "contact.html" form_cla ... ss = ContactForm success_url = "/thanks/" def form_valid ( self , form ): # This method is called when valid form data has been POSTed. # I ... ... ォルト値は、フォームの一部であるすべてのウィジェットの media 定義を追加した結果です: >>> from django import forms >>> class ContactForm ( forms . Form ): ... date = DateField ( widget = CalendarWidget ) ... name = CharField ( max_length = 40 , widget = Other ... Widget ) ... >>> f = ContactForm () >>> f . media <link href="https://static.example.com/pretty.css" media="all" rel="stylesheet"> <script src="https://sta ... フォームレイアウト用のCSSなど、フォームに追加のアセットを関連付けたい場合は、フォームに Media 宣言を追加します: >>> class ContactForm ( forms . Form ): ... date = DateField ( widget = CalendarWidget ) ... name = CharField ( max_length = 40 , widget = Other ... 2026年10月08日