diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
index 3cade52..4ff03cc 100644
a
|
b
|
|
1 | 1 | from django.forms import models as model_forms |
| 2 | from django.contrib import messages |
2 | 3 | from django.core.exceptions import ImproperlyConfigured |
3 | 4 | from django.http import HttpResponseRedirect |
| 5 | from django.utils.translation import ugettext_lazy as _ |
4 | 6 | from django.views.generic.base import TemplateResponseMixin, View |
5 | 7 | from django.views.generic.detail import (SingleObjectMixin, |
6 | 8 | SingleObjectTemplateResponseMixin, BaseDetailView) |
… |
… |
class FormView(TemplateResponseMixin, BaseFormView):
|
157 | 159 | """ |
158 | 160 | |
159 | 161 | |
160 | | class BaseCreateView(ModelFormMixin, ProcessFormView): |
| 162 | class SuccessMessageMixin(object): |
| 163 | """ |
| 164 | A mixin providing the ability to create success messages. |
| 165 | Message creation can be disabled by setting success_message=None. |
| 166 | """ |
| 167 | def get_success_message(self, **kwargs): |
| 168 | return self.success_message % kwargs |
| 169 | |
| 170 | def create_success_message(self, request, **kwargs): |
| 171 | try: |
| 172 | msg = self.get_success_message(**kwargs) |
| 173 | except AttributeError: |
| 174 | raise ImproperlyConfigured('`SuccessMessageMixin` subclasses must define a ' |
| 175 | '`success_message` or override `get_success_message`') |
| 176 | except TypeError: |
| 177 | if self.success_message is not None: |
| 178 | raise |
| 179 | else: |
| 180 | messages.success(request, msg, fail_silently=True) |
| 181 | |
| 182 | |
| 183 | class BaseCreateView(ModelFormMixin, ProcessFormView, SuccessMessageMixin): |
161 | 184 | """ |
162 | 185 | Base view for creating an new object instance. |
163 | 186 | |
164 | 187 | Using this base class requires subclassing to provide a response mixin. |
165 | 188 | """ |
| 189 | success_message = _("The %(verbose_name)s was created successfully.") |
| 190 | |
166 | 191 | def get(self, request, *args, **kwargs): |
167 | 192 | self.object = None |
168 | 193 | return super(BaseCreateView, self).get(request, *args, **kwargs) |
… |
… |
def get(self, request, *args, **kwargs):
|
170 | 195 | def post(self, request, *args, **kwargs): |
171 | 196 | self.object = None |
172 | 197 | return super(BaseCreateView, self).post(request, *args, **kwargs) |
| 198 | |
| 199 | def form_valid(self, form): |
| 200 | form_valid = super(BaseCreateView, self).form_valid(form) |
| 201 | self.create_success_message(self.request, object=self.object, |
| 202 | verbose_name=self.object._meta.verbose_name) |
| 203 | return form_valid |
173 | 204 | |
174 | 205 | |
175 | 206 | class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView): |
… |
… |
class CreateView(SingleObjectTemplateResponseMixin, BaseCreateView):
|
180 | 211 | template_name_suffix = '_form' |
181 | 212 | |
182 | 213 | |
183 | | class BaseUpdateView(ModelFormMixin, ProcessFormView): |
| 214 | class BaseUpdateView(ModelFormMixin, ProcessFormView, SuccessMessageMixin): |
184 | 215 | """ |
185 | 216 | Base view for updating an existing object. |
186 | 217 | |
187 | 218 | Using this base class requires subclassing to provide a response mixin. |
188 | 219 | """ |
| 220 | success_message = _("The %(verbose_name)s was updated successfully.") |
| 221 | |
189 | 222 | def get(self, request, *args, **kwargs): |
190 | 223 | self.object = self.get_object() |
191 | 224 | return super(BaseUpdateView, self).get(request, *args, **kwargs) |
… |
… |
def get(self, request, *args, **kwargs):
|
193 | 226 | def post(self, request, *args, **kwargs): |
194 | 227 | self.object = self.get_object() |
195 | 228 | return super(BaseUpdateView, self).post(request, *args, **kwargs) |
| 229 | |
| 230 | def form_valid(self, form): |
| 231 | form_valid = super(BaseUpdateView, self).form_valid(form) |
| 232 | self.create_success_message(self.request, object=self.object, |
| 233 | verbose_name=self.object._meta.verbose_name) |
| 234 | return form_valid |
196 | 235 | |
197 | 236 | |
198 | 237 | class UpdateView(SingleObjectTemplateResponseMixin, BaseUpdateView): |
… |
… |
def get_success_url(self):
|
226 | 265 | "No URL to redirect to. Provide a success_url.") |
227 | 266 | |
228 | 267 | |
229 | | class BaseDeleteView(DeletionMixin, BaseDetailView): |
| 268 | class BaseDeleteView(DeletionMixin, BaseDetailView, SuccessMessageMixin): |
230 | 269 | """ |
231 | 270 | Base view for deleting an object. |
232 | 271 | |
233 | 272 | Using this base class requires subclassing to provide a response mixin. |
234 | 273 | """ |
| 274 | success_message = _("The %(verbose_name)s was deleted.") |
| 275 | |
| 276 | def delete(self, request, *args, **kwargs): |
| 277 | response = super(BaseDeleteView, self).delete(request, *args, **kwargs) |
| 278 | self.create_success_message(request, object=self.object, |
| 279 | verbose_name=self.object._meta.verbose_name) |
| 280 | return response |
235 | 281 | |
236 | 282 | |
237 | 283 | class DeleteView(SingleObjectTemplateResponseMixin, BaseDeleteView): |