mirror of
https://github.com/django/django.git
synced 2025-10-28 08:06:09 +00:00
Deprecated current_app in TemplateResponse and render(_to_response).
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
from copy import copy
|
||||
import warnings
|
||||
|
||||
from django.utils.deprecation import RemovedInDjango20Warning
|
||||
|
||||
|
||||
# Hard-coded processor for easier use of CSRF protection.
|
||||
@@ -122,16 +125,23 @@ class Context(BaseContext):
|
||||
def __init__(self, dict_=None, autoescape=True,
|
||||
current_app=_current_app_undefined,
|
||||
use_l10n=None, use_tz=None, engine=None):
|
||||
if current_app is _current_app_undefined:
|
||||
current_app = None
|
||||
if current_app is not _current_app_undefined:
|
||||
warnings.warn(
|
||||
"The current_app argument of Context is deprecated. Use "
|
||||
"RequestContext and set the current_app attribute of its "
|
||||
"request instead.", RemovedInDjango20Warning, stacklevel=2)
|
||||
self.autoescape = autoescape
|
||||
self.current_app = current_app
|
||||
self._current_app = current_app
|
||||
self.use_l10n = use_l10n
|
||||
self.use_tz = use_tz
|
||||
self.engine = engine
|
||||
self.render_context = RenderContext()
|
||||
super(Context, self).__init__(dict_)
|
||||
|
||||
@property
|
||||
def current_app(self):
|
||||
return None if self._current_app is _current_app_undefined else self._current_app
|
||||
|
||||
def __copy__(self):
|
||||
duplicate = super(Context, self).__copy__()
|
||||
duplicate.render_context = copy(self.render_context)
|
||||
@@ -184,9 +194,17 @@ class RequestContext(Context):
|
||||
def __init__(self, request, dict_=None, processors=None,
|
||||
current_app=_current_app_undefined,
|
||||
use_l10n=None, use_tz=None, engine=None):
|
||||
Context.__init__(self, dict_, current_app=current_app,
|
||||
use_l10n=use_l10n, use_tz=use_tz, engine=engine)
|
||||
self._request = request
|
||||
# current_app isn't passed here to avoid triggering the deprecation
|
||||
# warning in Context.__init__.
|
||||
super(RequestContext, self).__init__(
|
||||
dict_, use_l10n=use_l10n, use_tz=use_tz, engine=engine)
|
||||
if current_app is not _current_app_undefined:
|
||||
warnings.warn(
|
||||
"The current_app argument of RequestContext is deprecated. "
|
||||
"Set the current_app attribute of its request instead.",
|
||||
RemovedInDjango20Warning, stacklevel=2)
|
||||
self._current_app = current_app
|
||||
self.request = request
|
||||
self._processors = () if processors is None else tuple(processors)
|
||||
self._processors_index = len(self.dicts)
|
||||
self.update({}) # placeholder for context processors output
|
||||
@@ -207,7 +225,7 @@ class RequestContext(Context):
|
||||
# Set context processors for this engine.
|
||||
updates = {}
|
||||
for processor in engine.template_context_processors + self._processors:
|
||||
updates.update(processor(self._request))
|
||||
updates.update(processor(self.request))
|
||||
self.dicts[self._processors_index] = updates
|
||||
|
||||
def new(self, values=None):
|
||||
|
||||
@@ -476,13 +476,20 @@ class URLNode(Node):
|
||||
|
||||
view_name = self.view_name.resolve(context)
|
||||
|
||||
try:
|
||||
current_app = context.request.current_app
|
||||
except AttributeError:
|
||||
# Change the fallback value to None when the deprecation path for
|
||||
# Context.current_app completes in Django 2.0.
|
||||
current_app = context.current_app
|
||||
|
||||
# Try to look up the URL twice: once given the view name, and again
|
||||
# relative to what we guess is the "main" app. If they both fail,
|
||||
# re-raise the NoReverseMatch unless we're using the
|
||||
# {% url ... as var %} construct in which case return nothing.
|
||||
url = ''
|
||||
try:
|
||||
url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
|
||||
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
|
||||
except NoReverseMatch:
|
||||
exc_info = sys.exc_info()
|
||||
if settings.SETTINGS_MODULE:
|
||||
@@ -490,7 +497,7 @@ class URLNode(Node):
|
||||
try:
|
||||
url = reverse(project_name + '.' + view_name,
|
||||
args=args, kwargs=kwargs,
|
||||
current_app=context.current_app)
|
||||
current_app=current_app)
|
||||
except NoReverseMatch:
|
||||
if self.asvar is None:
|
||||
# Re-raise the original exception, not the one with
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import warnings
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.template import loader, Context, RequestContext
|
||||
from django.template.context import _current_app_undefined
|
||||
from django.utils import six
|
||||
from django.utils.deprecation import RemovedInDjango20Warning
|
||||
|
||||
|
||||
class ContentNotRenderedError(Exception):
|
||||
@@ -137,14 +141,19 @@ class TemplateResponse(SimpleTemplateResponse):
|
||||
rendering_attrs = SimpleTemplateResponse.rendering_attrs + ['_request', '_current_app']
|
||||
|
||||
def __init__(self, request, template, context=None, content_type=None,
|
||||
status=None, current_app=None, charset=None):
|
||||
status=None, current_app=_current_app_undefined, charset=None):
|
||||
# self.request gets over-written by django.test.client.Client - and
|
||||
# unlike context_data and template_name the _request should not
|
||||
# be considered part of the public API.
|
||||
self._request = request
|
||||
# As a convenience we'll allow callers to provide current_app without
|
||||
# having to avoid needing to create the RequestContext directly
|
||||
self._current_app = current_app
|
||||
if current_app is not _current_app_undefined:
|
||||
warnings.warn(
|
||||
"The current_app argument of TemplateResponse is deprecated. "
|
||||
"Set the current_app attribute of its request instead.",
|
||||
RemovedInDjango20Warning, stacklevel=2)
|
||||
request.current_app = current_app
|
||||
super(TemplateResponse, self).__init__(
|
||||
template, context, content_type, status, charset)
|
||||
|
||||
@@ -154,7 +163,7 @@ class TemplateResponse(SimpleTemplateResponse):
|
||||
"""
|
||||
if isinstance(context, Context):
|
||||
return context
|
||||
context_instance = RequestContext(self._request, current_app=self._current_app)
|
||||
context_instance = RequestContext(self._request)
|
||||
if context:
|
||||
context_instance.push(context)
|
||||
return context_instance
|
||||
|
||||
Reference in New Issue
Block a user