Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14803 -- Corrected an inconsistency in redirection handling be…

…tween old-style generic views and class-based views. Thanks to gg for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14808 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 324658ef265fb9a4c5cc695a4b0f560575048604 1 parent ac91d5e
Russell Keith-Magee authored December 04, 2010
2  django/views/generic/edit.py
@@ -97,7 +97,7 @@ def get_form(self, form_class):
97 97
 
98 98
     def get_success_url(self):
99 99
         if self.success_url:
100  
-            url = self.success_url
  100
+            url = self.success_url % self.object.__dict__
101 101
         else:
102 102
             try:
103 103
                 url = self.object.get_absolute_url()
5  docs/ref/class-based-views.txt
@@ -481,6 +481,11 @@ ModelFormMixin
481 481
 
482 482
         The URL to redirect to when the form is successfully processed.
483 483
 
  484
+        ``success_url`` may contain dictionary string formatting, which
  485
+        will be interpolated against the object's field attributes. For
  486
+        example, you could use ``success_url="/polls/%(slug)s/"`` to
  487
+        redirect to a URL composed out of the ``slug`` field on a model.
  488
+
484 489
     .. method:: get_form_class()
485 490
 
486 491
         Retrieve the form class to instantiate. If
7  docs/topics/generic-views-migration.txt
@@ -113,6 +113,13 @@ For example::
113 113
             })
114 114
             return context
115 115
 
  116
+``post_save_redirect`` argument to create and update views
  117
+----------------------------------------------------------
  118
+
  119
+The ``post_save_redirect`` argument to the create and update views
  120
+has been renamed ``success_url`` on the
  121
+:class:`~django.views.generic.edit.ModelFormMixin`.
  122
+
116 123
 ``mimetype``
117 124
 ------------
118 125
 
20  tests/regressiontests/generic_views/edit.py
@@ -47,6 +47,14 @@ def test_create_with_redirect(self):
47 47
         self.assertRedirects(res, 'http://testserver/edit/authors/create/')
48 48
         self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
49 49
 
  50
+    def test_create_with_interpolated_redirect(self):
  51
+        res = self.client.post('/edit/authors/create/interpolate_redirect/',
  52
+                            {'name': 'Randall Munroe', 'slug': 'randall-munroe'})
  53
+        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
  54
+        self.assertEqual(res.status_code, 302)
  55
+        pk = Author.objects.all()[0].pk
  56
+        self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
  57
+
50 58
     def test_create_with_special_properties(self):
51 59
         res = self.client.get('/edit/authors/create/special/')
52 60
         self.assertEqual(res.status_code, 200)
@@ -145,6 +153,18 @@ def test_update_with_redirect(self):
145 153
         self.assertRedirects(res, 'http://testserver/edit/authors/create/')
146 154
         self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
147 155
 
  156
+    def test_update_with_interpolated_redirect(self):
  157
+        a = Author.objects.create(
  158
+            name='Randall Munroe',
  159
+            slug='randall-munroe',
  160
+        )
  161
+        res = self.client.post('/edit/author/%d/update/interpolate_redirect/' % a.pk,
  162
+                        {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'})
  163
+        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
  164
+        self.assertEqual(res.status_code, 302)
  165
+        pk = Author.objects.all()[0].pk
  166
+        self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk)
  167
+
148 168
     def test_update_with_special_properties(self):
149 169
         a = Author.objects.create(
150 170
             name='Randall Munroe',
6  tests/regressiontests/generic_views/urls.py
@@ -51,6 +51,8 @@
51 51
         views.NaiveAuthorCreate.as_view()),
52 52
     (r'^edit/authors/create/redirect/$',
53 53
         views.NaiveAuthorCreate.as_view(success_url='/edit/authors/create/')),
  54
+    (r'^edit/authors/create/interpolate_redirect/$',
  55
+        views.NaiveAuthorCreate.as_view(success_url='/edit/author/%(id)d/update/')),
54 56
     (r'^edit/authors/create/restricted/$',
55 57
         views.AuthorCreateRestricted.as_view()),
56 58
     (r'^edit/authors/create/$',
@@ -62,6 +64,8 @@
62 64
         views.NaiveAuthorUpdate.as_view()),
63 65
     (r'^edit/author/(?P<pk>\d+)/update/redirect/$',
64 66
         views.NaiveAuthorUpdate.as_view(success_url='/edit/authors/create/')),
  67
+    (r'^edit/author/(?P<pk>\d+)/update/interpolate_redirect/$',
  68
+        views.NaiveAuthorUpdate.as_view(success_url='/edit/author/%(id)d/update/')),
65 69
     (r'^edit/author/(?P<pk>\d+)/update/$',
66 70
         views.AuthorUpdate.as_view()),
67 71
     (r'^edit/author/(?P<pk>\d+)/update/special/$',
@@ -185,4 +189,4 @@
185 189
 
186 190
     # Useful for testing redirects
187 191
     (r'^accounts/login/$',  'django.contrib.auth.views.login')
188  
-)
  192
+)

0 notes on commit 324658e

Please sign in to comment.
Something went wrong with that request. Please try again.