Skip to content

Commit 9af569e

Browse files
committed
Reorganize object views
1 parent bd0507a commit 9af569e

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

plain/plain/views/objects.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88
from .templates import TemplateView
99

1010

11+
class CreateView(FormView):
12+
"""
13+
View for creating a new object, with a response rendered by a template.
14+
"""
15+
16+
# TODO? would rather you have to specify this...
17+
def get_success_url(self, form):
18+
"""Return the URL to redirect to after processing a valid form."""
19+
if self.success_url:
20+
url = self.success_url.format(**self.object.__dict__)
21+
else:
22+
try:
23+
url = self.object.get_absolute_url()
24+
except AttributeError:
25+
raise ImproperlyConfigured(
26+
"No URL to redirect to. Either provide a url or define"
27+
" a get_absolute_url method on the Model."
28+
)
29+
return url
30+
31+
def form_valid(self, form):
32+
"""If the form is valid, save the associated model."""
33+
self.object = form.save()
34+
return super().form_valid(form)
35+
36+
1137
class ObjectTemplateViewMixin:
1238
context_object_name = ""
1339

@@ -51,32 +77,6 @@ class DetailView(ObjectTemplateViewMixin, TemplateView):
5177
pass
5278

5379

54-
class CreateView(FormView):
55-
"""
56-
View for creating a new object, with a response rendered by a template.
57-
"""
58-
59-
# TODO? would rather you have to specify this...
60-
def get_success_url(self, form):
61-
"""Return the URL to redirect to after processing a valid form."""
62-
if self.success_url:
63-
url = self.success_url.format(**self.object.__dict__)
64-
else:
65-
try:
66-
url = self.object.get_absolute_url()
67-
except AttributeError:
68-
raise ImproperlyConfigured(
69-
"No URL to redirect to. Either provide a url or define"
70-
" a get_absolute_url method on the Model."
71-
)
72-
return url
73-
74-
def form_valid(self, form):
75-
"""If the form is valid, save the associated model."""
76-
self.object = form.save()
77-
return super().form_valid(form)
78-
79-
8080
class UpdateView(ObjectTemplateViewMixin, FormView):
8181
"""View for updating an object, with a response rendered by a template."""
8282

0 commit comments

Comments
 (0)