summaryrefslogtreecommitdiff
path: root/django/views
diff options
context:
space:
mode:
authorAdam Johnson <me@adamj.eu>2019-08-15 06:48:33 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-03-23 08:11:14 +0100
commit4ed534758cb6a11df9f49baddecca5a6cdda9311 (patch)
treeddfa4db02a7b6db0630352441bc1a92ceaa99a0f /django/views
parentf982f0bdb8317e75af416595c616943d5025da1e (diff)
Fixed #19878 -- Deprecated TemplateView passing URL kwargs into context.
Diffstat (limited to 'django/views')
-rw-r--r--django/views/generic/base.py30
1 files changed, 26 insertions, 4 deletions
diff --git a/django/views/generic/base.py b/django/views/generic/base.py
index 3dd957d8f8..ea5baca08d 100644
--- a/django/views/generic/base.py
+++ b/django/views/generic/base.py
@@ -1,4 +1,5 @@
import logging
+import warnings
from functools import update_wrapper
from django.core.exceptions import ImproperlyConfigured
@@ -9,6 +10,8 @@ from django.http import (
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.decorators import classonlymethod
+from django.utils.deprecation import RemovedInDjango40Warning
+from django.utils.functional import SimpleLazyObject
logger = logging.getLogger('django.request')
@@ -152,14 +155,33 @@ class TemplateResponseMixin:
class TemplateView(TemplateResponseMixin, ContextMixin, View):
- """
- Render a template. Pass keyword arguments from the URLconf to the context.
- """
+ """Render a template."""
def get(self, request, *args, **kwargs):
- context = self.get_context_data(**kwargs)
+ # RemovedInDjango40Warning: when the deprecation ends, replace with:
+ # context = self.get_context_data()
+ context_kwargs = _wrap_url_kwargs_with_deprecation_warning(kwargs)
+ context = self.get_context_data(**context_kwargs)
return self.render_to_response(context)
+# RemovedInDjango40Warning
+def _wrap_url_kwargs_with_deprecation_warning(url_kwargs):
+ context_kwargs = {}
+ for key, value in url_kwargs.items():
+ # Bind into function closure.
+ @SimpleLazyObject
+ def access_value(key=key, value=value):
+ warnings.warn(
+ 'TemplateView passing URL kwargs to the context is '
+ 'deprecated. Reference %s in your template through '
+ 'view.kwargs instead.' % key,
+ RemovedInDjango40Warning, stacklevel=2,
+ )
+ return value
+ context_kwargs[key] = access_value
+ return context_kwargs
+
+
class RedirectView(View):
"""Provide a redirect on any GET request."""
permanent = False