|
3 | 3 | of MVC. In other words, these functions/classes introduce controlled coupling
|
4 | 4 | for convenience's sake.
|
5 | 5 | """
|
6 |
| -import warnings |
7 |
| - |
8 | 6 | from django.template import loader, RequestContext
|
9 | 7 | from django.template.context import _current_app_undefined
|
10 | 8 | from django.template.engine import (
|
|
16 | 14 | from django.db.models.query import QuerySet
|
17 | 15 | from django.core import urlresolvers
|
18 | 16 | from django.utils import six
|
19 |
| -from django.utils.deprecation import RemovedInDjango20Warning |
20 | 17 |
|
21 | 18 |
|
22 |
| -def render_to_response(template_name, dictionary=_dictionary_undefined, |
| 19 | +def render_to_response(template_name, context=None, |
23 | 20 | context_instance=_context_instance_undefined,
|
24 |
| - content_type=None, dirs=_dirs_undefined): |
| 21 | + content_type=None, status=None, dirs=_dirs_undefined, |
| 22 | + dictionary=_dictionary_undefined): |
25 | 23 | """
|
26 | 24 | Returns a HttpResponse whose content is filled with the result of calling
|
27 | 25 | django.template.loader.render_to_string() with the passed arguments.
|
28 | 26 | """
|
29 |
| - # TODO: refactor to avoid the deprecated code path. |
30 |
| - with warnings.catch_warnings(): |
31 |
| - warnings.filterwarnings("ignore", category=RemovedInDjango20Warning) |
32 |
| - content = loader.render_to_string(template_name, dictionary, context_instance, dirs) |
| 27 | + if (context_instance is _context_instance_undefined |
| 28 | + and dirs is _dirs_undefined |
| 29 | + and dictionary is _dictionary_undefined): |
| 30 | + # No deprecated arguments were passed - use the new code path |
| 31 | + content = loader.get_template(template_name).render(context) |
| 32 | + |
| 33 | + else: |
| 34 | + # Some deprecated arguments were passed - use the legacy code path |
| 35 | + content = loader.render_to_string( |
| 36 | + template_name, context, context_instance, dirs, dictionary) |
33 | 37 |
|
34 |
| - return HttpResponse(content, content_type) |
| 38 | + return HttpResponse(content, content_type, status) |
35 | 39 |
|
36 | 40 |
|
37 |
| -def render(request, template_name, dictionary=_dictionary_undefined, |
| 41 | +def render(request, template_name, context=None, |
38 | 42 | context_instance=_context_instance_undefined,
|
39 | 43 | content_type=None, status=None, current_app=_current_app_undefined,
|
40 |
| - dirs=_dirs_undefined): |
| 44 | + dirs=_dirs_undefined, dictionary=_dictionary_undefined): |
41 | 45 | """
|
42 | 46 | Returns a HttpResponse whose content is filled with the result of calling
|
43 | 47 | django.template.loader.render_to_string() with the passed arguments.
|
44 | 48 | Uses a RequestContext by default.
|
45 | 49 | """
|
46 |
| - if context_instance is not _context_instance_undefined: |
47 |
| - if current_app is not _current_app_undefined: |
48 |
| - raise ValueError('If you provide a context_instance you must ' |
49 |
| - 'set its current_app before calling render()') |
| 50 | + if (context_instance is _context_instance_undefined |
| 51 | + and current_app is _current_app_undefined |
| 52 | + and dirs is _dirs_undefined |
| 53 | + and dictionary is _dictionary_undefined): |
| 54 | + # No deprecated arguments were passed - use the new code path |
| 55 | + content = loader.get_template(template_name).render(context, request) |
| 56 | + |
50 | 57 | else:
|
51 |
| - context_instance = RequestContext(request, current_app=current_app) |
| 58 | + # Some deprecated arguments were passed - use the legacy code path |
| 59 | + if context_instance is not _context_instance_undefined: |
| 60 | + if current_app is not _current_app_undefined: |
| 61 | + raise ValueError('If you provide a context_instance you must ' |
| 62 | + 'set its current_app before calling render()') |
| 63 | + else: |
| 64 | + context_instance = RequestContext(request, current_app=current_app) |
52 | 65 |
|
53 |
| - # TODO: refactor to avoid the deprecated code path. |
54 |
| - with warnings.catch_warnings(): |
55 |
| - warnings.filterwarnings("ignore", category=RemovedInDjango20Warning) |
56 |
| - content = loader.render_to_string(template_name, dictionary, context_instance, dirs) |
| 66 | + content = loader.render_to_string( |
| 67 | + template_name, context, context_instance, dirs, dictionary) |
57 | 68 |
|
58 | 69 | return HttpResponse(content, content_type, status)
|
59 | 70 |
|
|
0 commit comments