Django 1.8.2 文档

通用的编辑视图

本页中描述的以下视图提供内容编辑的基础功能:

本页中的一些示例假定Author 模型已经在myapp/models.py 中定义如下:

from django.core.urlresolvers import reverse
from django.db import models

class Author(models.Model):
    name = models.CharField(max_length=200)

    def get_absolute_url(self):
        return reverse('author-detail', kwargs={'pk': self.pk})

FormView

class django.views.generic.edit.FormView

显示表单的视图。发送错误时,重新显示表单和验证的错误;成功时,重定向到一个新的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
        pass

示例 myapp/views.py

from myapp.forms import ContactForm
from django.views.generic.edit import FormView

class ContactView(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(ContactView, self).form_valid(form)

示例 myapp/contact.html

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Send message" />
</form>

CreateView

class django.views.generic.edit.CreateView

一个视图,它显示一个表单用于创建对象,出现错误时会重新显示表单,否则保存该对象。

祖先(MRO)

该视图从以下视图继承方法和属性:

属性

template_name_suffix

显示给GET请求的CreateView页使用'_ form'template_name_suffix例如,对于为示例Author模型创建对象的视图,将此属性更改为'_ create_form'会导致默认template_name'myapp/author_create_form.html'

object

使用CreateView时,您可以访问self.object,这是正在被创建的对象。如果对象尚未创建,则值将为None

示例 myapp/views.py

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

class AuthorCreate(CreateView):
    model = Author
    fields = ['name']

示例 myapp/author_form.html

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Create" />
</form>

UpdateView

class django.views.generic.edit.UpdateView

A view that displays a form for editing an existing object, redisplaying the form with validation errors (if there are any) and saving changes to the object. This uses a form automatically generated from the object’s model class (unless a form class is manually specified).

祖先(MRO)

该视图从以下视图继承方法和属性:

属性

template_name_suffix

The UpdateView page displayed to a GET request uses a template_name_suffix of '_form'. For example, changing this attribute to '_update_form' for a view updating objects for the example Author model would cause the default template_name to be 'myapp/author_update_form.html'.

object

When using UpdateView you have access to self.object, which is the object being updated.

示例 myapp/views.py

from django.views.generic.edit import UpdateView
from myapp.models import Author

class AuthorUpdate(UpdateView):
    model = Author
    fields = ['name']
    template_name_suffix = '_update_form'

示例 myapp/author_update_form.html

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Update" />
</form>

DeleteView

class django.views.generic.edit.DeleteView

A view that displays a confirmation page and deletes an existing object. The given object will only be deleted if the request method is POST. If this view is fetched via GET, it will display a confirmation page that should contain a form that POSTs to the same URL.

祖先(MRO)

该视图从以下视图继承方法和属性:

属性

template_name_suffix

The DeleteView page displayed to a GET request uses a template_name_suffix of '_confirm_delete'. For example, changing this attribute to '_check_delete' for a view deleting objects for the example Author model would cause the default template_name to be 'myapp/author_check_delete.html'.

示例 myapp/views.py

from django.views.generic.edit import DeleteView
from django.core.urlresolvers import reverse_lazy
from myapp.models import Author

class AuthorDelete(DeleteView):
    model = Author
    success_url = reverse_lazy('author-list')

示例 myapp/author_confirm_delete.html

<form action="" method="post">{% csrf_token %}
    <p>Are you sure you want to delete "{{ object }}"?</p>
    <input type="submit" value="Confirm" />
</form>