2021-11-09 16:03:40 +08:00
|
|
|
import warnings
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2021-07-13 22:06:12 +08:00
|
|
|
from django.forms import Form
|
|
|
|
from django.forms import models as model_forms
|
2010-10-18 21:34:47 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
|
|
|
|
from django.views.generic.detail import (
|
|
|
|
BaseDetailView,
|
|
|
|
SingleObjectMixin,
|
|
|
|
SingleObjectTemplateResponseMixin,
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
2015-09-04 02:41:54 +08:00
|
|
|
class FormMixin(ContextMixin):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Provide a way to show and handle a form in a request."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
initial = {}
|
|
|
|
form_class = None
|
|
|
|
success_url = None
|
2013-06-22 19:12:43 +08:00
|
|
|
prefix = None
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_initial(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the initial data to use for forms on this view."""
|
2012-03-18 06:31:03 +08:00
|
|
|
return self.initial.copy()
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2013-06-22 19:12:43 +08:00
|
|
|
def get_prefix(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the prefix to use for forms."""
|
2013-06-22 19:12:43 +08:00
|
|
|
return self.prefix
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def get_form_class(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the form class to use."""
|
2010-10-18 21:34:47 +08:00
|
|
|
return self.form_class
|
|
|
|
|
2014-10-15 02:56:39 +08:00
|
|
|
def get_form(self, form_class=None):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return an instance of the form to be used in this view."""
|
2014-10-15 02:56:39 +08:00
|
|
|
if form_class is None:
|
|
|
|
form_class = self.get_form_class()
|
2010-12-10 11:51:30 +08:00
|
|
|
return form_class(**self.get_form_kwargs())
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the keyword arguments for instantiating the form."""
|
2013-06-22 19:12:43 +08:00
|
|
|
kwargs = {
|
|
|
|
"initial": self.get_initial(),
|
|
|
|
"prefix": self.get_prefix(),
|
|
|
|
}
|
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
if self.request.method in ("POST", "PUT"):
|
2010-12-10 11:51:30 +08:00
|
|
|
kwargs.update(
|
|
|
|
{
|
|
|
|
"data": self.request.POST,
|
|
|
|
"files": self.request.FILES,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return kwargs
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_success_url(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the URL to redirect to after processing a valid form."""
|
2017-04-22 01:52:26 +08:00
|
|
|
if not self.success_url:
|
|
|
|
raise ImproperlyConfigured("No URL to redirect to. Provide a success_url.")
|
|
|
|
return str(self.success_url) # success_url may be lazy
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""If the form is valid, redirect to the supplied URL."""
|
2010-10-18 21:34:47 +08:00
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
def form_invalid(self, form):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""If the form is invalid, render the invalid form."""
|
2015-11-10 21:06:59 +08:00
|
|
|
return self.render_to_response(self.get_context_data(form=form))
|
2015-04-15 04:50:36 +08:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Insert the form into the context dict."""
|
2015-12-31 05:22:58 +08:00
|
|
|
if "form" not in kwargs:
|
|
|
|
kwargs["form"] = self.get_form()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get_context_data(**kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ModelFormMixin(FormMixin, SingleObjectMixin):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Provide a way to show and handle a ModelForm in a request."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2013-06-24 05:43:09 +08:00
|
|
|
fields = None
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_form_class(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the form class to use in this view."""
|
2014-11-15 19:17:55 +08:00
|
|
|
if self.fields is not None and self.form_class:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Specifying both 'fields' and 'form_class' is not permitted."
|
|
|
|
)
|
2010-10-18 21:34:47 +08:00
|
|
|
if self.form_class:
|
|
|
|
return self.form_class
|
|
|
|
else:
|
2011-02-15 16:12:29 +08:00
|
|
|
if self.model is not None:
|
|
|
|
# If a model has been explicitly provided, use it
|
|
|
|
model = self.model
|
2018-01-04 00:34:10 +08:00
|
|
|
elif getattr(self, "object", None) is not None:
|
2011-02-15 16:12:29 +08:00
|
|
|
# If this view is operating on a single object, use
|
|
|
|
# the class of that object
|
|
|
|
model = self.object.__class__
|
|
|
|
else:
|
|
|
|
# Try to get a queryset and extract the model class
|
|
|
|
# from that
|
|
|
|
model = self.get_queryset().model
|
2013-02-22 05:56:55 +08:00
|
|
|
|
2013-06-24 05:43:09 +08:00
|
|
|
if self.fields is None:
|
2014-03-22 08:44:34 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Using ModelFormMixin (base class of %s) without "
|
|
|
|
"the 'fields' attribute is prohibited." % self.__class__.__name__
|
|
|
|
)
|
2013-02-22 05:56:55 +08:00
|
|
|
|
2013-06-24 05:43:09 +08:00
|
|
|
return model_forms.modelform_factory(model, fields=self.fields)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2010-12-10 11:51:30 +08:00
|
|
|
def get_form_kwargs(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the keyword arguments for instantiating the form."""
|
2017-01-21 21:13:44 +08:00
|
|
|
kwargs = super().get_form_kwargs()
|
2014-02-09 18:53:23 +08:00
|
|
|
if hasattr(self, "object"):
|
|
|
|
kwargs.update({"instance": self.object})
|
2010-12-10 11:51:30 +08:00
|
|
|
return kwargs
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_success_url(self):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Return the URL to redirect to after processing a valid form."""
|
2010-10-18 21:34:47 +08:00
|
|
|
if self.success_url:
|
2015-09-03 06:36:09 +08:00
|
|
|
url = self.success_url.format(**self.object.__dict__)
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
url = self.object.get_absolute_url()
|
|
|
|
except AttributeError:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"No URL to redirect to. Either provide a url or define"
|
|
|
|
" a get_absolute_url method on the Model."
|
|
|
|
)
|
|
|
|
return url
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""If the form is valid, save the associated model."""
|
2010-10-18 21:34:47 +08:00
|
|
|
self.object = form.save()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().form_valid(form)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ProcessFormView(View):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Render a form on GET and processes it on POST."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def get(self, request, *args, **kwargs):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Handle GET requests: instantiate a blank version of the form."""
|
2015-04-15 04:50:36 +08:00
|
|
|
return self.render_to_response(self.get_context_data())
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Handle POST requests: instantiate a form instance with the passed
|
|
|
|
POST variables and then check if it's valid.
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
2014-10-15 02:56:39 +08:00
|
|
|
form = self.get_form()
|
2010-10-18 21:34:47 +08:00
|
|
|
if form.is_valid():
|
|
|
|
return self.form_valid(form)
|
|
|
|
else:
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
|
|
|
# PUT is a valid HTTP verb for creating (with a known URL) or editing an
|
|
|
|
# object, note that browsers only support POST for now.
|
2010-10-28 10:59:04 +08:00
|
|
|
def put(self, *args, **kwargs):
|
|
|
|
return self.post(*args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BaseFormView(FormMixin, ProcessFormView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""A base view for displaying a form."""
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FormView(TemplateResponseMixin, BaseFormView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""A view for displaying a form and rendering a template response."""
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BaseCreateView(ModelFormMixin, ProcessFormView):
|
|
|
|
"""
|
2017-11-07 11:11:39 +08:00
|
|
|
Base view for creating a new object instance.
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
Using this base class requires subclassing to provide a response mixin.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.object = None
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get(request, *args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
self.object = None
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().post(request, *args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-01-27 11:14:20 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
View for creating a new object, with a response rendered by a template.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = "_form"
|
|
|
|
|
|
|
|
|
|
|
|
class BaseUpdateView(ModelFormMixin, ProcessFormView):
|
|
|
|
"""
|
|
|
|
Base view for updating an existing object.
|
|
|
|
|
|
|
|
Using this base class requires subclassing to provide a response mixin.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
def get(self, request, *args, **kwargs):
|
2010-10-20 08:21:47 +08:00
|
|
|
self.object = self.get_object()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().get(request, *args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2010-10-20 08:21:47 +08:00
|
|
|
self.object = self.get_object()
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().post(request, *args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
|
|
|
|
class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView):
|
2017-01-25 04:36:07 +08:00
|
|
|
"""View for updating an object, with a response rendered by a template."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = "_form"
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class DeletionMixin:
|
2017-01-25 04:36:07 +08:00
|
|
|
"""Provide the ability to delete objects."""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
success_url = None
|
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
Call the delete() method on the fetched object and then redirect to the
|
|
|
|
success URL.
|
2012-06-11 16:34:00 +08:00
|
|
|
"""
|
2010-10-20 08:21:47 +08:00
|
|
|
self.object = self.get_object()
|
2013-02-11 15:39:14 +08:00
|
|
|
success_url = self.get_success_url()
|
2010-10-18 21:34:47 +08:00
|
|
|
self.object.delete()
|
2013-02-11 15:39:14 +08:00
|
|
|
return HttpResponseRedirect(success_url)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
# Add support for browsers which only accept GET and POST for now.
|
2013-06-11 18:40:06 +08:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
return self.delete(request, *args, **kwargs)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
if self.success_url:
|
2015-09-03 06:36:09 +08:00
|
|
|
return self.success_url.format(**self.object.__dict__)
|
2010-10-18 21:34:47 +08:00
|
|
|
else:
|
2010-10-19 15:22:38 +08:00
|
|
|
raise ImproperlyConfigured("No URL to redirect to. Provide a success_url.")
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-01-27 11:14:20 +08:00
|
|
|
|
2021-11-09 16:03:40 +08:00
|
|
|
# RemovedInDjango50Warning.
|
|
|
|
class DeleteViewCustomDeleteWarning(Warning):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-07-13 22:06:12 +08:00
|
|
|
class BaseDeleteView(DeletionMixin, FormMixin, BaseDetailView):
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
|
|
|
Base view for deleting an object.
|
|
|
|
|
|
|
|
Using this base class requires subclassing to provide a response mixin.
|
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2021-07-13 22:06:12 +08:00
|
|
|
form_class = Form
|
|
|
|
|
2021-11-09 16:03:40 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
# RemovedInDjango50Warning.
|
|
|
|
if self.__class__.delete is not DeletionMixin.delete:
|
|
|
|
warnings.warn(
|
|
|
|
f"DeleteView uses FormMixin to handle POST requests. As a "
|
|
|
|
f"consequence, any custom deletion logic in "
|
|
|
|
f"{self.__class__.__name__}.delete() handler should be moved "
|
|
|
|
f"to form_valid().",
|
|
|
|
DeleteViewCustomDeleteWarning,
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2021-07-13 22:06:12 +08:00
|
|
|
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)
|
2010-10-18 21:34:47 +08:00
|
|
|
|
2011-01-27 11:14:20 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView):
|
|
|
|
"""
|
2017-01-25 04:36:07 +08:00
|
|
|
View for deleting an object retrieved with self.get_object(), with a
|
|
|
|
response rendered by a template.
|
2010-10-18 21:34:47 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2010-10-18 21:34:47 +08:00
|
|
|
template_name_suffix = "_confirm_delete"
|