Generic editing ビューLink to this heading
以下のビューは、このページで説明され、内容を編集するための基礎を提供します:
FormViewLink to this heading
- class django.views.generic.edit.FormViewLink to this definition
フォームを描画するビューです。エラー時には、バリデーションエラーとともにフォームを再描画します。成功時には、新しい URL にリダイレクトします。
継承元 (MRO)
このメソッドは、以下のビューからメソッドと属性を継承しています:
例 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 passExample 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. # It should return an HttpResponse. form.send_email() return super().form_valid(form)例 myapp/forms.py:
<form method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="Send message"> </form>
- class django.views.generic.edit.BaseFormViewLink to this definition
A base view for displaying a form. It is not intended to be used directly, but rather as a parent class of the
django.views.generic.edit.FormViewor other views displaying a form.継承元 (MRO)
このメソッドは、以下のビューからメソッドと属性を継承しています:
CreateViewLink to this heading
- class django.views.generic.edit.CreateViewLink to this definition
オブジェクトの作成、(もしある場合は) バリデーションエラーとフォームの再描画、そしてオブジェクトの保存のフォームを表示するビューです。
継承元 (MRO)
このメソッドは、以下のビューからメソッドと属性を継承しています:
属性
- template_name_suffixLink to this definition
GETリクエストに描画されたCreateViewのページは、'_form'のtemplate_name_suffixを使います。 例えば、この属性をオブジェクト (例えばAuthorモデル) を作成するビューのために'_create_form'に変更すると、 デフォルトのtemplate_nameは'myapp/author_create_form.html'となります。
- objectLink to this definition
CreateViewを使うとき、self.objectにアクセスできます。これは作成されているオブジェクトです。オブジェクトがまだ作成されていない場合、値はNoneになります。
Example myapp/views.py:
from django.views.generic.edit import CreateView from myapp.models import Author class AuthorCreateView(CreateView): model = Author fields = ["name"]例 myapp/author_form.html:
<form method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="Save"> </form>
- class django.views.generic.edit.BaseCreateViewLink to this definition
A base view for creating a new object instance. It is not intended to be used directly, but rather as a parent class of the
django.views.generic.edit.CreateView.継承元 (MRO)
このメソッドは、以下のビューからメソッドと属性を継承しています:
メソッド
- get(request, *args, **kwargs)Link to this definition
Sets the current object instance (
self.object) toNone.
- post(request, *args, **kwargs)Link to this definition
Sets the current object instance (
self.object) toNone.
UpdateViewLink to this heading
- class django.views.generic.edit.UpdateViewLink to this definition
現存するオブジェクトの編集、(もしある場合は) バリデーションエラーとフォームの再描画、そしてオブジェクトの保存のフォームを表示するビューです。(フォームのクラスが手動で指定されていない限り) オブジェクトのモデルのクラスから自動的に生成されたフォームを使います。
継承元 (MRO)
このメソッドは、以下のビューからメソッドと属性を継承しています:
属性
- template_name_suffixLink to this definition
GETリクエストに描画されたUpdateViewのページは、'_form'のtemplate_name_suffixを使います。 例えば、この属性をオブジェクト (例えばAuthorモデル) を作成するビューのために'_update_form'に変更すると、 デフォルトのtemplate_nameは'myapp/author_update_form.html'となります。
- objectLink to this definition
UpdateViewを使うとき、self.objectにアクセスできます。これは更新されているオブジェクトです。
Example myapp/views.py:
from django.views.generic.edit import UpdateView from myapp.models import Author class AuthorUpdateView(UpdateView): model = Author fields = ["name"] template_name_suffix = "_update_form"例 myapp/author_update_form.html:
<form method="post">{% csrf_token %} {{ form.as_p }} <input type="submit" value="Update"> </form>
- class django.views.generic.edit.BaseUpdateViewLink to this definition
A base view for updating an existing object instance. It is not intended to be used directly, but rather as a parent class of the
django.views.generic.edit.UpdateView.継承元 (MRO)
このメソッドは、以下のビューからメソッドと属性を継承しています:
メソッド
- get(request, *args, **kwargs)Link to this definition
Sets the current object instance (
self.object).
- post(request, *args, **kwargs)Link to this definition
Sets the current object instance (
self.object).
DeleteViewLink to this heading
- class django.views.generic.edit.DeleteViewLink to this definition
確認ページを表示して、現存するオブジェクトを削除するビューです。与えられたオブジェクトは、リクエストメソッドが
POSTの場合、単に削除されます。もしこのビューがGETを通じて取得された場合は、同じ URL に POST するフォームを含む確認画面を表示します。継承元 (MRO)
このメソッドは、以下のビューからメソッドと属性を継承しています:
属性
- form_classLink to this definition
Inherited from
BaseDeleteView. The form class that will be used to confirm the request. By defaultdjango.forms.Form, resulting in an empty form that is always valid.By providing your own
Formsubclass, you can add additional requirements, such as a confirmation checkbox, for example.
- template_name_suffixLink to this definition
GETリクエストに描画されたDeleteViewのページは、'_confirm_delete'のtemplate_name_suffixを使います。 例えば、この属性をオブジェクト (例えばAuthorモデル) を作成するビューのために'_check_delete'に変更すると、 デフォルトのtemplate_nameは'myapp/author_check_delete.html'となります。
Example myapp/views.py:
from django.urls import reverse_lazy from django.views.generic.edit import DeleteView from myapp.models import Author class AuthorDeleteView(DeleteView): model = Author success_url = reverse_lazy("author-list")例 myapp/author_confirm_delete.html:
<form method="post">{% csrf_token %} <p>Are you sure you want to delete "{{ object }}"?</p> {{ form }} <input type="submit" value="Confirm"> </form>
- class django.views.generic.edit.BaseDeleteViewLink to this definition
A base view for deleting an object instance. It is not intended to be used directly, but rather as a parent class of the
django.views.generic.edit.DeleteView.継承元 (MRO)
このメソッドは、以下のビューからメソッドと属性を継承しています: