Code

Ticket #19378: 19378-1.diff

File 19378-1.diff, 4.5 KB (added by claudep, 17 months ago)

Force reverse_lazy evaluation

Line 
1diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py
2index e51cdf5..97a6c0a 100644
3--- a/django/views/generic/edit.py
4+++ b/django/views/generic/edit.py
5@@ -1,6 +1,7 @@
6 from django.forms import models as model_forms
7 from django.core.exceptions import ImproperlyConfigured
8 from django.http import HttpResponseRedirect
9+from django.utils.encoding import force_text
10 from django.views.generic.base import TemplateResponseMixin, ContextMixin, View
11 from django.views.generic.detail import (SingleObjectMixin,
12                         SingleObjectTemplateResponseMixin, BaseDetailView)
13@@ -50,7 +51,8 @@ class FormMixin(ContextMixin):
14         Returns the supplied success URL.
15         """
16         if self.success_url:
17-            url = self.success_url
18+            # Forcing possible reverse_lazy evaluation
19+            url = force_text(self.success_url)
20         else:
21             raise ImproperlyConfigured(
22                 "No URL to redirect to. Provide a success_url.")
23diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py
24index 16f4da8..0f1eb3c 100644
25--- a/tests/regressiontests/generic_views/edit.py
26+++ b/tests/regressiontests/generic_views/edit.py
27@@ -20,6 +20,15 @@ class FormMixinTests(TestCase):
28          initial_2 = FormMixin().get_initial()
29          self.assertNotEqual(initial_1, initial_2)
30 
31+
32+class BasicFormTests(TestCase):
33+    urls = 'regressiontests.generic_views.urls'
34+
35+    def test_post_data(self):
36+        res = self.client.post('/contact/', {'name': "Me", 'message': "Hello"})
37+        self.assertRedirects(res, 'http://testserver/list/authors/')
38+
39+
40 class ModelFormMixinTests(TestCase):
41     def test_get_form(self):
42         form_class = views.AuthorGetQuerySetFormView().get_form_class()
43diff --git a/tests/regressiontests/generic_views/forms.py b/tests/regressiontests/generic_views/forms.py
44index a78242f..e036ad8 100644
45--- a/tests/regressiontests/generic_views/forms.py
46+++ b/tests/regressiontests/generic_views/forms.py
47@@ -11,3 +11,8 @@ class AuthorForm(forms.ModelForm):
48 
49     class Meta:
50         model = Author
51+
52+
53+class ContactForm(forms.Form):
54+    name = forms.CharField()
55+    message = forms.CharField(widget=forms.Textarea)
56diff --git a/tests/regressiontests/generic_views/tests.py b/tests/regressiontests/generic_views/tests.py
57index c985ad3..3f80600 100644
58--- a/tests/regressiontests/generic_views/tests.py
59+++ b/tests/regressiontests/generic_views/tests.py
60@@ -6,6 +6,6 @@ from .dates import (ArchiveIndexViewTests, YearArchiveViewTests,
61     MonthArchiveViewTests, WeekArchiveViewTests, DayArchiveViewTests,
62     DateDetailViewTests)
63 from .detail import DetailViewTest
64-from .edit import (FormMixinTests, ModelFormMixinTests, CreateViewTests,
65-    UpdateViewTests, DeleteViewTests)
66+from .edit import (FormMixinTests, BasicFormTests, ModelFormMixinTests,
67+    CreateViewTests, UpdateViewTests, DeleteViewTests)
68 from .list import ListViewTests
69diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py
70index a212b83..9caf193 100644
71--- a/tests/regressiontests/generic_views/urls.py
72+++ b/tests/regressiontests/generic_views/urls.py
73@@ -56,6 +56,10 @@ urlpatterns = patterns('',
74     (r'^detail/nonmodel/1/$',
75         views.NonModelDetail.as_view()),
76 
77+    # FormView
78+    (r'^contact/$',
79+        views.ContactView.as_view()),
80+
81     # Create/UpdateView
82     (r'^edit/artists/create/$',
83         views.ArtistCreate.as_view()),
84diff --git a/tests/regressiontests/generic_views/views.py b/tests/regressiontests/generic_views/views.py
85index f7fcf6f..71e78e8 100644
86--- a/tests/regressiontests/generic_views/views.py
87+++ b/tests/regressiontests/generic_views/views.py
88@@ -2,11 +2,11 @@ from __future__ import absolute_import
89 
90 from django.contrib.auth.decorators import login_required
91 from django.core.paginator import Paginator
92-from django.core.urlresolvers import reverse
93+from django.core.urlresolvers import reverse, reverse_lazy
94 from django.utils.decorators import method_decorator
95 from django.views import generic
96 
97-from .forms import AuthorForm
98+from .forms import AuthorForm, ContactForm
99 from .models import Artist, Author, Book, Page, BookSigning
100 
101 
102@@ -75,6 +75,13 @@ class AuthorListCustomPaginator(AuthorList):
103             orphans=2,
104             allow_empty_first_page=allow_empty_first_page)
105 
106+
107+class ContactView(generic.FormView):
108+    form_class = ContactForm
109+    success_url = reverse_lazy('authors_list')
110+    template_name = 'generic_views/form.html'
111+
112+
113 class ArtistCreate(generic.CreateView):
114     model = Artist
115