Simplified implementation of django.shortcuts.render(_to_response).

*args, **kwargs brought more confusion than concision.
This commit is contained in:
Aymeric Augustin 2014-12-13 17:41:00 +01:00
parent 92e8f1f302
commit a0141f9eac
2 changed files with 24 additions and 21 deletions

View File

@ -6,6 +6,9 @@ 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 (
_context_instance_undefined, _dictionary_undefined, _dirs_undefined)
from django.http import HttpResponse, Http404
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
from django.db.models.base import ModelBase
@ -16,49 +19,43 @@ from django.utils import six
from django.utils.deprecation import RemovedInDjango20Warning
def render_to_response(*args, **kwargs):
def render_to_response(template_name, dictionary=_dictionary_undefined,
context_instance=_context_instance_undefined,
content_type=None, dirs=_dirs_undefined):
"""
Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
httpresponse_kwargs = {'content_type': kwargs.pop('content_type', None)}
# TODO: refactor to avoid the deprecated code path.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
content = loader.render_to_string(*args, **kwargs)
content = loader.render_to_string(template_name, dictionary, context_instance, dirs)
return HttpResponse(content, **httpresponse_kwargs)
return HttpResponse(content, content_type)
def render(request, *args, **kwargs):
def render(request, template_name, dictionary=_dictionary_undefined,
context_instance=_context_instance_undefined,
content_type=None, status=None, current_app=_current_app_undefined,
dirs=_dirs_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.
"""
httpresponse_kwargs = {
'content_type': kwargs.pop('content_type', None),
'status': kwargs.pop('status', None),
}
if 'context_instance' in kwargs:
context_instance = kwargs.pop('context_instance')
if kwargs.get('current_app', None):
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:
current_app = kwargs.pop('current_app', None)
context_instance = RequestContext(request, current_app=current_app)
kwargs['context_instance'] = context_instance
# TODO: refactor to avoid the deprecated code path.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
content = loader.render_to_string(*args, **kwargs)
content = loader.render_to_string(template_name, dictionary, context_instance, dirs)
return HttpResponse(content, **httpresponse_kwargs)
return HttpResponse(content, content_type, status)
def redirect(to, *args, **kwargs):

View File

@ -4,6 +4,8 @@ from copy import copy
# Hard-coded processor for easier use of CSRF protection.
_builtin_context_processors = ('django.template.context_processors.csrf',)
_current_app_undefined = object()
class ContextPopException(Exception):
"pop() has been called more times than push()"
@ -117,8 +119,11 @@ class BaseContext(object):
class Context(BaseContext):
"A stack container for variable context"
def __init__(self, dict_=None, autoescape=True, current_app=None,
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
self.autoescape = autoescape
self.current_app = current_app
self.use_l10n = use_l10n
@ -176,7 +181,8 @@ class RequestContext(Context):
Additional processors can be specified as a list of callables
using the "processors" keyword argument.
"""
def __init__(self, request, dict_=None, processors=None, current_app=None,
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)