|
8 | 8 | from .templates import TemplateView
|
9 | 9 |
|
10 | 10 |
|
| 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 | + |
11 | 37 | class ObjectTemplateViewMixin:
|
12 | 38 | context_object_name = ""
|
13 | 39 |
|
@@ -51,32 +77,6 @@ class DetailView(ObjectTemplateViewMixin, TemplateView):
|
51 | 77 | pass
|
52 | 78 |
|
53 | 79 |
|
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 |
| - |
80 | 80 | class UpdateView(ObjectTemplateViewMixin, FormView):
|
81 | 81 | """View for updating an object, with a response rendered by a template."""
|
82 | 82 |
|
|
0 commit comments