... グできます。 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 ... ... を用意しています。 基本的なフォーム 以下のようなコンタクトフォームがあったとします。 forms.py from django import forms class ContactForm ( forms . Form ): name = forms . CharField () message = forms . CharField ( widget = forms . Textarea ) def send_email ( s ... self.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 ... ... ォルト値は、フォームの一部であるすべてのウィジェットの 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 ... ... 用的なフォームを考えましょう。個人のウェブサイトで "お問い合わせ" 機能を実装します: forms.py from django import forms class ContactForm ( forms . Form ): subject = forms . CharField ( max_length = 100 ) message = forms . CharField ( widget = forms . Textarea ... ます。 注釈 この時点では、 request.POST から検証前のデータを取り出せますが、検証済みのデータを使うのがよい方法です。 In the contact form example above, urgent will be a boolean value. Likewise, fields such as IntegerField and FloatField convert values to a P ... _email = form . cleaned_data [ "contact_email" ] urgent = form . cleaned_data [ "urgent" ] body = ( "Submitted through the contact form. \n " f "Contact email (unverified): { contact_email } \n " f "Marked urgent: { 'yes' if urgent else 'no' } \n " f "Messa ... ... ix Field. label_suffix label_suffix 引数を使用すると、フォームの label_suffix をフィールドごとに上書きできます。 >>> class ContactForm ( forms . Form ): ... age = forms . IntegerField () ... nationality = forms . CharField () ... captcha_answer = forms . In ... tegerField ( label = "2 + 2" , label_suffix = " =" ) ... >>> f = ContactForm ( label_suffix = "?" ) >>> print ( f ) <div><label for="id_age">Age?</label><input type="number" name="age" required id="i ... Form の完全な例です。出力を単純化するために auto_id=False を指定しています: >>> from django import forms >>> class HelpTextContactForm ( forms . Form ): ... subject = forms . CharField ( max_length = 100 , help_text = "100 characters max." ) ... message = f ... 2026年10月08日