... グできます。 class Form [ソース] バインドされていない Form のインスタンスを作成するには、クラスの初期化を行います。 >>> f = ContactForm () フォームにデータをバインドするには、辞書型のデータを Form クラスのコンストラクタの最初のパラメータとして渡します。 ... >>> data = { ... "subject" : "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 空の辞書を渡すと、データ ... ... by 属性をビュークラスに追加します。以下に例を示します。 from django.views.generic import ListView from myapp.models import Contact class ContactListView ( ListView ): paginate_by = 2 model = Contact これにより、各ページのオブジェクト数が制限され、 pagin ... ユーザーがページ間を移動できるように、次のようにしてテンプレートに次のページと前のページへのリンクを追加しましょう。 {% for contact in page_obj %} {# Each "contact" is a Contact model object. #} {{ contact.full_name | upper }} < br > ... {% endfor %} < d ... ator の一例です。 from django.core.paginator import Paginator from django.shortcuts import render from myapp.models import Contact def listing ( request ): contact_list = Contact . objects . all () paginator = Paginator ( contact_list , 25 ) # Show 25 c ... ... 用的なフォームを考えましょう。個人のウェブサイトで "お問い合わせ" 機能を実装します: forms.py from django import forms class ContactForm ( forms . Form ): subject = forms . CharField ( max_length = 100 ) message = forms . CharField ( widget = forms . Text ... area ) contact_email = forms . EmailField () urgent = forms . BooleanField ( required = False ) Our earlier form used a single field, you ... r_name , a CharField . In this case, our form has four fields: subject , message , contact_email and urgent . CharField , EmailField and BooleanField are just three of the available field types; a full list can be ... ... import Redirect >>> # Add a new redirect. >>> redirect = Redirect . objects . create ( ... site_id = 1 , ... old_path = "/contact-us/" , ... new_path = "/contact/" , ... ) >>> # Change a redirect. >>> redirect . new_path = "/contact-details/" >>> redir ... ect . save () >>> redirect <Redirect: /contact-us/ ---> /contact-details/> >>> # Delete a redirect. >>> Redirect . objects . filter ( site_id = 1 , old_path = "/contact- ... ... 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 ... ( self ): # 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 ... _class = ContactForm success_url = "/thanks/" def form_valid ( self , form ): # This method is called when valid form data has been POSTed. ... ... 。これはこのタイプのフィールドに特有であり、それはこの後の使われ方には関係ありません。 このフィールドの使い方を示すために、 ContactForm を作成してみましょう: class ContactForm ( forms . Form ): subject = forms . CharField ( max_length = 100 ) message = f ... orms . CharField () contact_email = forms . EmailField () recipients = MultiEmailField () urgent = forms . BooleanField ( required = False ) MultiEmai ... メソッドを呼び出します。 特定のフィールド属性をクリーニングする 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 th ... ... ix Field. label_suffix label_suffix 引数を使用すると、フォームの label_suffix をフィールドごとに上書きできます。 >>> class ContactForm ( forms . Form ): ... age = forms . IntegerField () ... nationality = forms . CharField () ... captcha_answer = forms ... . IntegerField ( label = "2 + 2" , label_suffix = " =" ) ... >>> f = ContactForm ( label_suffix = "?" ) >>> print ( f ) <div><label for="id_age">Age?</label><input type="number" name="age" required 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 ... ... able to be claimed. Otherwise, somebody else may be working on this ticket. Either find another bug/feature to work on, or contact the developer working on the ticket to offer your help. If a ticket has been assigned for weeks or months without any acti ... ... オブジェクトでラップします。例えば、 JSONField に対してデフォルトの dict を指定したい場合、下記のような関数を使用します: def contact_default (): return { "email" : "to1@example.com" } contact_info = JSONField ( "ContactInfo" , default = contact_default ) ... ... を用意しています。 基本的なフォーム 以下のようなコンタクトフォームがあったとします。 forms.py from django import forms class ContactForm ( forms . Form ): name = forms . CharField () message = forms . CharField ( widget = forms . Textarea ) def send_email ... 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 ... _class = ContactForm success_url = "/thanks/" def form_valid ( self , form ): # This method is called when valid form data has been POSTed. ... 2026年10月08日