Generic editing views

    参见

    The messages framework contains, whichfacilitates presenting messages about successful form submissions.

    注解

    Some of the examples on this page assume that an Author model has beendefined as follows in myapp/models.py:

    • class django.views.generic.edit.FormView
    • A view that displays a form. On error, redisplays the form with validationerrors; on success, redirects to a new URL.

    Ancestors (MRO)

    This view inherits methods and attributes from the following views:

    1. from django import forms
    2.  
    3. class ContactForm(forms.Form):
    4. name = forms.CharField()
    5. message = forms.CharField(widget=forms.Textarea)
    6.  
    7. def send_email(self):
    8. # send email using the self.cleaned_data dictionary
    9. pass

    Example myapp/views.py:

    1. from myapp.forms import ContactForm
    2. from django.views.generic.edit import FormView
    3.  
    4. class ContactView(FormView):
    5. template_name = 'contact.html'
    6. form_class = ContactForm
    7. success_url = '/thanks/'
    8. def form_valid(self, form):
    9. # This method is called when valid form data has been POSTed.
    10. # It should return an HttpResponse.
    11. form.send_email()
    12. return super().form_valid(form)

    Example myapp/contact.html:

    • class django.views.generic.edit.CreateView
    • A view that displays a form for creating an object, redisplaying the formwith validation errors (if there are any) and saving the object.

    Ancestors (MRO)

    Example myapp/views.py:

    1. from django.views.generic.edit import CreateView
    2. from myapp.models import Author
    3.  
    4. class AuthorCreate(CreateView):
    5. model = Author
    6. fields = ['name']

    Example myapp/author_form.html:

    1. <form method="post">{% csrf_token %}
    2. {{ form.as_p }}
    3. <input type="submit" value="Save">
    4. </form>
    • class django.views.generic.edit.UpdateView
    • A view that displays a form for editing an existing object, redisplayingthe form with validation errors (if there are any) and saving changes tothe object. This uses a form automatically generated from the object'smodel class (unless a form class is manually specified).

    Ancestors (MRO)

    This view inherits methods and attributes from the following views:

    Example myapp/views.py:

    Example myapp/author_update_form.html:

    1. <form method="post">{% csrf_token %}
    2. {{ form.as_p }}
    3. <input type="submit" value="Update">
    4. </form>
    • 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. Ifthis view is fetched via GET, it will display a confirmation page thatshould contain a form that POSTs to the same URL.

    Ancestors (MRO)

    This view inherits methods and attributes from the following views:

    Example myapp/views.py:

    1. from django.urls import reverse_lazy
    2. from django.views.generic.edit import DeleteView
    3. from myapp.models import Author
    4.  
    5. class AuthorDelete(DeleteView):
    6. success_url = reverse_lazy('author-list')