From 324658ef265fb9a4c5cc695a4b0f560575048604 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 4 Dec 2010 11:20:30 +0000 Subject: [PATCH] Fixed #14803 -- Corrected an inconsistency in redirection handling between 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 --- django/views/generic/edit.py | 2 +- docs/ref/class-based-views.txt | 5 +++++ docs/topics/generic-views-migration.txt | 7 +++++++ tests/regressiontests/generic_views/edit.py | 20 ++++++++++++++++++++ tests/regressiontests/generic_views/urls.py | 6 +++++- 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/django/views/generic/edit.py b/django/views/generic/edit.py index d607091f55dc9..efdf0ce97c439 100644 --- a/django/views/generic/edit.py +++ b/django/views/generic/edit.py @@ -97,7 +97,7 @@ def get_form(self, form_class): def get_success_url(self): if self.success_url: - url = self.success_url + url = self.success_url % self.object.__dict__ else: try: url = self.object.get_absolute_url() diff --git a/docs/ref/class-based-views.txt b/docs/ref/class-based-views.txt index 758763f84560d..28ce0244831ba 100644 --- a/docs/ref/class-based-views.txt +++ b/docs/ref/class-based-views.txt @@ -481,6 +481,11 @@ ModelFormMixin The URL to redirect to when the form is successfully processed. + ``success_url`` may contain dictionary string formatting, which + will be interpolated against the object's field attributes. For + example, you could use ``success_url="/polls/%(slug)s/"`` to + redirect to a URL composed out of the ``slug`` field on a model. + .. method:: get_form_class() Retrieve the form class to instantiate. If diff --git a/docs/topics/generic-views-migration.txt b/docs/topics/generic-views-migration.txt index 407ef24e49d88..bf3a5f2b62a29 100644 --- a/docs/topics/generic-views-migration.txt +++ b/docs/topics/generic-views-migration.txt @@ -113,6 +113,13 @@ For example:: }) return context +``post_save_redirect`` argument to create and update views +---------------------------------------------------------- + +The ``post_save_redirect`` argument to the create and update views +has been renamed ``success_url`` on the +:class:`~django.views.generic.edit.ModelFormMixin`. + ``mimetype`` ------------ diff --git a/tests/regressiontests/generic_views/edit.py b/tests/regressiontests/generic_views/edit.py index 2579b2af78a8d..7991ef0d04eb4 100644 --- a/tests/regressiontests/generic_views/edit.py +++ b/tests/regressiontests/generic_views/edit.py @@ -47,6 +47,14 @@ def test_create_with_redirect(self): self.assertRedirects(res, 'http://testserver/edit/authors/create/') self.assertQuerysetEqual(Author.objects.all(), ['']) + def test_create_with_interpolated_redirect(self): + res = self.client.post('/edit/authors/create/interpolate_redirect/', + {'name': 'Randall Munroe', 'slug': 'randall-munroe'}) + self.assertQuerysetEqual(Author.objects.all(), ['']) + self.assertEqual(res.status_code, 302) + pk = Author.objects.all()[0].pk + self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk) + def test_create_with_special_properties(self): res = self.client.get('/edit/authors/create/special/') self.assertEqual(res.status_code, 200) @@ -145,6 +153,18 @@ def test_update_with_redirect(self): self.assertRedirects(res, 'http://testserver/edit/authors/create/') self.assertQuerysetEqual(Author.objects.all(), ['']) + def test_update_with_interpolated_redirect(self): + a = Author.objects.create( + name='Randall Munroe', + slug='randall-munroe', + ) + res = self.client.post('/edit/author/%d/update/interpolate_redirect/' % a.pk, + {'name': 'Randall Munroe (author of xkcd)', 'slug': 'randall-munroe'}) + self.assertQuerysetEqual(Author.objects.all(), ['']) + self.assertEqual(res.status_code, 302) + pk = Author.objects.all()[0].pk + self.assertRedirects(res, 'http://testserver/edit/author/%d/update/' % pk) + def test_update_with_special_properties(self): a = Author.objects.create( name='Randall Munroe', diff --git a/tests/regressiontests/generic_views/urls.py b/tests/regressiontests/generic_views/urls.py index 61f0387331045..0643b400ff686 100644 --- a/tests/regressiontests/generic_views/urls.py +++ b/tests/regressiontests/generic_views/urls.py @@ -51,6 +51,8 @@ views.NaiveAuthorCreate.as_view()), (r'^edit/authors/create/redirect/$', views.NaiveAuthorCreate.as_view(success_url='/edit/authors/create/')), + (r'^edit/authors/create/interpolate_redirect/$', + views.NaiveAuthorCreate.as_view(success_url='/edit/author/%(id)d/update/')), (r'^edit/authors/create/restricted/$', views.AuthorCreateRestricted.as_view()), (r'^edit/authors/create/$', @@ -62,6 +64,8 @@ views.NaiveAuthorUpdate.as_view()), (r'^edit/author/(?P\d+)/update/redirect/$', views.NaiveAuthorUpdate.as_view(success_url='/edit/authors/create/')), + (r'^edit/author/(?P\d+)/update/interpolate_redirect/$', + views.NaiveAuthorUpdate.as_view(success_url='/edit/author/%(id)d/update/')), (r'^edit/author/(?P\d+)/update/$', views.AuthorUpdate.as_view()), (r'^edit/author/(?P\d+)/update/special/$', @@ -185,4 +189,4 @@ # Useful for testing redirects (r'^accounts/login/$', 'django.contrib.auth.views.login') -) \ No newline at end of file +)