Ticket #14803: 14803.2.diff

File 14803.2.diff, 4.9 KB (added by Gabriel Grant, 13 years ago)

patch implements solution #2. Includes tests & docs

  • django/views/generic/edit.py

     
    9797
    9898    def get_success_url(self):
    9999        if self.success_url:
    100             url = self.success_url
     100            url = self.success_url % self.object.__dict__
    101101        else:
    102102            try:
    103103                url = self.object.get_absolute_url()
  • tests/regressiontests/generic_views/edit.py

     
    4747        self.assertRedirects(res, 'http://testserver/edit/authors/create/')
    4848        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe>'])
    4949
     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
    5058    def test_create_with_special_properties(self):
    5159        res = self.client.get('/edit/authors/create/special/')
    5260        self.assertEqual(res.status_code, 200)
     
    145153        self.assertRedirects(res, 'http://testserver/edit/authors/create/')
    146154        self.assertQuerysetEqual(Author.objects.all(), ['<Author: Randall Munroe (author of xkcd)>'])
    147155
     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
    148168    def test_update_with_special_properties(self):
    149169        a = Author.objects.create(
    150170            name='Randall Munroe',
  • tests/regressiontests/generic_views/urls.py

     
    5151        views.NaiveAuthorCreate.as_view()),
    5252    (r'^edit/authors/create/redirect/$',
    5353        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/')),
    5456    (r'^edit/authors/create/restricted/$',
    5557        views.AuthorCreateRestricted.as_view()),
    5658    (r'^edit/authors/create/$',
     
    6264        views.NaiveAuthorUpdate.as_view()),
    6365    (r'^edit/author/(?P<pk>\d+)/update/redirect/$',
    6466        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/')),
    6569    (r'^edit/author/(?P<pk>\d+)/update/$',
    6670        views.AuthorUpdate.as_view()),
    6771    (r'^edit/author/(?P<pk>\d+)/update/special/$',
     
    185189
    186190    # Useful for testing redirects
    187191    (r'^accounts/login/$',  'django.contrib.auth.views.login')
    188 )
    189  No newline at end of file
     192)
  • docs/topics/generic-views-migration.txt

     
    113113            })
    114114            return context
    115115
     116``post_save_redirect`` argument to create and update views
     117----------------------------------------------------------
     118
     119The ``post_save_redirect`` argument to the create and update views
     120has been renamed ``context_object_name`` on the
     121:class:`~django.views.generic.edit.ModelFormMixin`.
     122
    116123``mimetype``
    117124------------
    118125
  • docs/ref/class-based-views.txt

     
    481481
    482482        The URL to redirect to when the form is successfully processed.
    483483
     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/"``.
     487
    484488    .. method:: get_form_class()
    485489
    486490        Retrieve the form class to instantiate. If
Back to Top