diff options
| author | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-14 10:17:18 +0100 |
|---|---|---|
| committer | Aymeric Augustin <aymeric.augustin@m4x.org> | 2014-12-28 17:02:29 +0100 |
| commit | fdbfc98003f0ba2d3a12def63a75560791f3602d (patch) | |
| tree | 0238fba169970595f1a8c44d53550fada6236b4f /django/shortcuts.py | |
| parent | a0141f9eac03f0fef722e757253bbc939c018f77 (diff) | |
Deprecated some arguments of django.shortcuts.render(_to_response).
dictionary and context_instance and superseded by context.
Refactored tests that relied context_instance with more modern idioms.
Diffstat (limited to 'django/shortcuts.py')
| -rw-r--r-- | django/shortcuts.py | 53 |
1 files changed, 32 insertions, 21 deletions
diff --git a/django/shortcuts.py b/django/shortcuts.py index f3c07a6972..780f89a6b5 100644 --- a/django/shortcuts.py +++ b/django/shortcuts.py @@ -3,8 +3,6 @@ This module collects helper functions and classes that "span" multiple levels of MVC. In other words, these functions/classes introduce controlled coupling for convenience's sake. """ -import warnings - from django.template import loader, RequestContext from django.template.context import _current_app_undefined from django.template.engine import ( @@ -16,44 +14,57 @@ from django.db.models.manager import Manager from django.db.models.query import QuerySet from django.core import urlresolvers from django.utils import six -from django.utils.deprecation import RemovedInDjango20Warning -def render_to_response(template_name, dictionary=_dictionary_undefined, +def render_to_response(template_name, context=None, context_instance=_context_instance_undefined, - content_type=None, dirs=_dirs_undefined): + content_type=None, status=None, dirs=_dirs_undefined, + dictionary=_dictionary_undefined): """ Returns a HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments. """ - # TODO: refactor to avoid the deprecated code path. - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=RemovedInDjango20Warning) - content = loader.render_to_string(template_name, dictionary, context_instance, dirs) + if (context_instance is _context_instance_undefined + and dirs is _dirs_undefined + and dictionary is _dictionary_undefined): + # No deprecated arguments were passed - use the new code path + content = loader.get_template(template_name).render(context) + + else: + # Some deprecated arguments were passed - use the legacy code path + content = loader.render_to_string( + template_name, context, context_instance, dirs, dictionary) - return HttpResponse(content, content_type) + return HttpResponse(content, content_type, status) -def render(request, template_name, dictionary=_dictionary_undefined, +def render(request, template_name, context=None, context_instance=_context_instance_undefined, content_type=None, status=None, current_app=_current_app_undefined, - dirs=_dirs_undefined): + dirs=_dirs_undefined, dictionary=_dictionary_undefined): """ Returns a HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments. Uses a RequestContext by default. """ - if context_instance is not _context_instance_undefined: - if current_app is not _current_app_undefined: - raise ValueError('If you provide a context_instance you must ' - 'set its current_app before calling render()') + if (context_instance is _context_instance_undefined + and current_app is _current_app_undefined + and dirs is _dirs_undefined + and dictionary is _dictionary_undefined): + # No deprecated arguments were passed - use the new code path + content = loader.get_template(template_name).render(context, request) + else: - context_instance = RequestContext(request, current_app=current_app) + # Some deprecated arguments were passed - use the legacy code path + if context_instance is not _context_instance_undefined: + if current_app is not _current_app_undefined: + raise ValueError('If you provide a context_instance you must ' + 'set its current_app before calling render()') + else: + context_instance = RequestContext(request, current_app=current_app) - # TODO: refactor to avoid the deprecated code path. - with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=RemovedInDjango20Warning) - content = loader.render_to_string(template_name, dictionary, context_instance, dirs) + content = loader.render_to_string( + template_name, context, context_instance, dirs, dictionary) return HttpResponse(content, content_type, status) |
