Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save the instance namespace during the resolving process as a 'current_app' request attribute #260

Closed
wants to merge 7 commits into from
5 changes: 3 additions & 2 deletions django/core/handlers/base.py
Expand Up @@ -100,8 +100,9 @@ def get_response(self, request):
urlresolvers.set_urlconf(urlconf)
resolver = urlresolvers.RegexURLResolver(r'^/', urlconf)

callback, callback_args, callback_kwargs = resolver.resolve(
request.path_info)
match = resolver.resolve(request.path_info)
callback, callback_args, callback_kwargs = match
request.current_app = match.namespace if match.namespace else None

# Apply view middleware
for middleware_method in self._view_middleware:
Expand Down
2 changes: 1 addition & 1 deletion django/http/__init__.py
Expand Up @@ -160,6 +160,7 @@ def __init__(self):
self.path = ''
self.path_info = ''
self.method = None
self.current_app = None
self._post_parse_error = False

def __repr__(self):
Expand Down Expand Up @@ -756,4 +757,3 @@ def str_to_unicode(s, encoding):
return six.text_type(s, encoding, 'replace')
else:
return s

2 changes: 1 addition & 1 deletion django/shortcuts/__init__.py
Expand Up @@ -36,7 +36,7 @@ def render(request, *args, **kwargs):
raise ValueError('If you provide a context_instance you must '
'set its current_app before calling render()')
else:
current_app = kwargs.pop('current_app', None)
current_app = kwargs.pop('current_app', request.current_app)
context_instance = RequestContext(request, current_app=current_app)

kwargs['context_instance'] = context_instance
Expand Down
3 changes: 2 additions & 1 deletion docs/topics/http/shortcuts.txt
Expand Up @@ -58,7 +58,8 @@ Optional arguments
``current_app``
A hint indicating which application contains the current view. See the
:ref:`namespaced URL resolution strategy <topics-http-reversing-url-namespaces>`
for more information.
for more information. This value defaults to the ``current_app`` attribute
of the ``request`` argument.

Example
-------
Expand Down
4 changes: 4 additions & 0 deletions docs/topics/http/urls.txt
Expand Up @@ -549,6 +549,10 @@ instance namespace. For example, the ``urls`` attribute of Django's
all the patterns in an admin site, plus the name of the admin instance, and the
application namespace ``admin``.

Requests objects passed to your application views carry an additional
``current_app`` attribute conaining the instance namespace for the URL pattern
that matched the requested URL.

Once you have defined namespaced URLs, you can reverse them. For details on
reversing namespaced urls, see the documentation on :ref:`reversing namespaced
URLs <topics-http-reversing-url-namespaces>`.
Expand Down
1 change: 1 addition & 0 deletions tests/regressiontests/requests/tests.py
Expand Up @@ -21,6 +21,7 @@ def test_httprequest(self):
self.assertEqual(request.POST.keys(), [])
self.assertEqual(request.COOKIES.keys(), [])
self.assertEqual(request.META.keys(), [])
self.assertEqual(request.current_app, None)

def test_httprequest_repr(self):
request = HttpRequest()
Expand Down