Ticket #13411: 13411_comment_redirect_with_anchor.diff

File 13411_comment_redirect_with_anchor.diff, 2.4 KB (added by Julien Phalip, 13 years ago)
  • django/contrib/comments/views/utils.py

    diff --git a/django/contrib/comments/views/utils.py b/django/contrib/comments/views/utils.py
    index 8b729d2..cc985e5 100644
    a b def next_redirect(data, default, default_view, **get_kwargs):  
    2525    if next is None:
    2626        next = urlresolvers.reverse(default_view)
    2727    if get_kwargs:
     28        if '#' in next:
     29            tmp = next.rsplit('#', 1)
     30            next = tmp[0]
     31            anchor = '#' + tmp[1]
     32        else:
     33            anchor = ''
     34
    2835        joiner = ('?' in next) and '&' or '?'
    29         next += joiner + urllib.urlencode(get_kwargs)
     36        next += joiner + urllib.urlencode(get_kwargs) + anchor
    3037    return HttpResponseRedirect(next)
    3138
    3239def confirmation_view(template, doc="Display a confirmation view."):
  • tests/regressiontests/comment_tests/tests/comment_view_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/comment_view_tests.py b/tests/regressiontests/comment_tests/tests/comment_view_tests.py
    index b8a62b4..5e79196 100644
    a b class CommentViewTests(CommentTestCase):  
    256256        broken_location = location + u"\ufffd"
    257257        response = self.client.get(broken_location)
    258258        self.assertEqual(response.status_code, 200)
     259
     260    def testCommentNextWithQueryStringAndAnchor(self):
     261        """
     262        The `next` key needs to handle already having an anchor. Refs #13411.
     263        """
     264        # With a query string also.
     265        a = Article.objects.get(pk=1)
     266        data = self.getValidData(a)
     267        data["next"] = "/somewhere/else/?foo=bar#baz"
     268        data["comment"] = "This is another comment"
     269        response = self.client.post("/post/", data)
     270        location = response["Location"]
     271        match = re.search(r"^http://testserver/somewhere/else/\?foo=bar&c=\d+#baz$", location)
     272        self.failUnless(match != None, "Unexpected redirect location: %s" % location)
     273
     274        # Without a query string
     275        a = Article.objects.get(pk=1)
     276        data = self.getValidData(a)
     277        data["next"] = "/somewhere/else/#baz"
     278        data["comment"] = "This is another comment"
     279        response = self.client.post("/post/", data)
     280        location = response["Location"]
     281        match = re.search(r"^http://testserver/somewhere/else/\?c=\d+#baz$", location)
     282        self.failUnless(match != None, "Unexpected redirect location: %s" % location)
Back to Top