mirror of
https://github.com/django/django.git
synced 2025-10-30 00:56:09 +00:00
Fixed #21936 -- Allowed DeleteView to work with custom Forms and SuccessMessageMixin.
Thanks to Mariusz Felisiak for review. Co-authored-by: Demetris Stavrou <demestav@gmail.com> Co-authored-by: Caroline Simpson <github@hoojiboo.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.forms import models as model_forms
|
||||
from django.forms import Form, models as model_forms
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
|
||||
from django.views.generic.detail import (
|
||||
@@ -225,12 +225,30 @@ class DeletionMixin:
|
||||
"No URL to redirect to. Provide a success_url.")
|
||||
|
||||
|
||||
class BaseDeleteView(DeletionMixin, BaseDetailView):
|
||||
class BaseDeleteView(DeletionMixin, FormMixin, BaseDetailView):
|
||||
"""
|
||||
Base view for deleting an object.
|
||||
|
||||
Using this base class requires subclassing to provide a response mixin.
|
||||
"""
|
||||
form_class = Form
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
# Set self.object before the usual form processing flow.
|
||||
# Inlined because having DeletionMixin as the first base, for
|
||||
# get_success_url(), makes leveraging super() with ProcessFormView
|
||||
# overly complex.
|
||||
self.object = self.get_object()
|
||||
form = self.get_form()
|
||||
if form.is_valid():
|
||||
return self.form_valid(form)
|
||||
else:
|
||||
return self.form_invalid(form)
|
||||
|
||||
def form_valid(self, form):
|
||||
success_url = self.get_success_url()
|
||||
self.object.delete()
|
||||
return HttpResponseRedirect(success_url)
|
||||
|
||||
|
||||
class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):
|
||||
|
||||
Reference in New Issue
Block a user