diff --git a/django/http/request.py b/django/http/request.py index 28eae377a9..5a14c2a41a 100644 --- a/django/http/request.py +++ b/django/http/request.py @@ -113,11 +113,17 @@ class HttpRequest: return str(port) def get_full_path(self, force_append_slash=False): + return self._get_full_path(self.path, force_append_slash) + + def get_full_path_info(self, force_append_slash=False): + return self._get_full_path(self.path_info, force_append_slash) + + def _get_full_path(self, path, force_append_slash): # RFC 3986 requires query string arguments to be in the ASCII range. # Rather than crash if this doesn't happen, we encode defensively. return '%s%s%s' % ( - escape_uri_path(self.path), - '/' if force_append_slash and not self.path.endswith('/') else '', + escape_uri_path(path), + '/' if force_append_slash and not path.endswith('/') else '', ('?' + iri_to_uri(self.META.get('QUERY_STRING', ''))) if self.META.get('QUERY_STRING', '') else '' ) diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index 850a30dfc9..0a5f1b610c 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -284,6 +284,15 @@ Methods Example: ``"/music/bands/the_beatles/?print=true"`` +.. method:: HttpRequest.get_full_path_info() + + .. versionadded:: 2.1 + + Like :meth:`get_full_path`, but uses :attr:`path_info` instead of + :attr:`path`. + + Example: ``"/minfo/music/bands/the_beatles/?print=true"`` + .. method:: HttpRequest.build_absolute_uri(location) Returns the absolute URI form of ``location``. If no location is provided, diff --git a/docs/releases/2.1.txt b/docs/releases/2.1.txt index 2135e1a958..bb8090e34b 100644 --- a/docs/releases/2.1.txt +++ b/docs/releases/2.1.txt @@ -159,7 +159,7 @@ Models Requests and Responses ~~~~~~~~~~~~~~~~~~~~~~ -* ... +* Added :meth:`.HttpRequest.get_full_path_info`. Serialization ~~~~~~~~~~~~~ diff --git a/tests/requests/tests.py b/tests/requests/tests.py index bc99818797..4c26bccf4e 100644 --- a/tests/requests/tests.py +++ b/tests/requests/tests.py @@ -38,16 +38,20 @@ class RequestsTests(SimpleTestCase): def test_httprequest_full_path(self): request = HttpRequest() - request.path = request.path_info = '/;some/?awful/=path/foo:bar/' + request.path = '/;some/?awful/=path/foo:bar/' + request.path_info = '/prefix' + request.path request.META['QUERY_STRING'] = ';some=query&+query=string' expected = '/%3Bsome/%3Fawful/%3Dpath/foo:bar/?;some=query&+query=string' self.assertEqual(request.get_full_path(), expected) + self.assertEqual(request.get_full_path_info(), '/prefix' + expected) def test_httprequest_full_path_with_query_string_and_fragment(self): request = HttpRequest() - request.path = request.path_info = '/foo#bar' + request.path = '/foo#bar' + request.path_info = '/prefix' + request.path request.META['QUERY_STRING'] = 'baz#quux' self.assertEqual(request.get_full_path(), '/foo%23bar?baz#quux') + self.assertEqual(request.get_full_path_info(), '/prefix/foo%23bar?baz#quux') def test_httprequest_repr(self): request = HttpRequest()