Search This Blog

More Editing Views - Python

 

More Editing Views

The form-based views can be extended for creating, updating, and deleting objects on the database.

This is done using,

  • CreateView - View that displays a form for creating, saving an object and redisplaying the form with validation errors.
  • UpdateView - View that displays a form generated from an object's model class for editing, saving an existing object and redisplaying the form with validation errors.
  • DeleteView - View that displays a confirmation page and deletes an existing object based on a POST request.

Usage in views.py

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

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

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

No comments:

Post a Comment

If you have any doubts, Please let us know.