Generic editing ビューLink to this heading

以下のビューは、このページで説明され、内容を編集するための基礎を提供します:

FormViewLink to this heading

class django.views.generic.edit.FormViewLink to this definition

フォームを描画するビューです。エラー時には、バリデーションエラーとともにフォームを再描画します。成功時には、新しい URL にリダイレクトします。

継承元 (MRO)

このメソッドは、以下のビューからメソッドと属性を継承しています:

例 myapp/forms.py:

Code
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

Example myapp/views.py:

Code
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:

Django template
<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.FormView or 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:

Code
from django.views.generic.edit import CreateView
from myapp.models import Author


class AuthorCreateView(CreateView):
    model = Author
    fields = ["name"]

例 myapp/author_form.html:

Django template
<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) to None.

post(request, *args, **kwargs)Link to this definition

Sets the current object instance (self.object) to None.

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:

Code
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:

Django template
<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 default django.forms.Form, resulting in an empty form that is always valid.

By providing your own Form subclass, 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:

Code
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:

Django template
<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)

このメソッドは、以下のビューからメソッドと属性を継承しています: