太长不看
当应用上下文处理器时
当您使用RequestContext
时,您直接提供的变量首先被添加,然后是由上下文处理器提供的任何变量。这意味着上下文处理器可能会覆盖您提供的变量,因此请注意避免与上下文处理器提供的变量重叠的变量名称。
首先让我们看一下方法render_to_response
和render
的定义。
def render_to_response(*args, **kwargs):
"""
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)}
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
def render(request, *args, **kwargs):
"""
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):
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
return HttpResponse(loader.render_to_string(*args, **kwargs),
**httpresponse_kwargs)
有没有更好的方法来强制使用RequestContext作为默认值?
注意在子类化上下文:RequestContext
部分中
如果您使用Django的render_to_response()
快捷方式将字典内容填充到模板中,则默认情况下会传递Context
实例而不是RequestContext
从上面的代码中,方法
render_to_response
调用了方法
loader.render_to_string
,其中在
这一行 检查了
context_instance
参数。
方法
render_to_string
的代码清单。
def render_to_string(template_name, dictionary=None, context_instance=None,
dirs=None):
"""
Loads the given template_name and renders it with the given dictionary as
context. The template_name may be a string to load a single template using
get_template, or it may be a tuple to use select_template to find one of
the templates in the list. Returns a string.
"""
dictionary = dictionary or {}
if isinstance(template_name, (list, tuple)):
t = select_template(template_name, dirs)
else:
t = get_template(template_name, dirs)
if not context_instance:
return t.render(Context(dictionary))
with context_instance.push(dictionary):
return t.render(context_instance)
我们能否用render
编写简单易用的装饰器呢?
我们可以编写这个装饰器,但是你的问题是主观的。很难说它是否易于使用。这在很大程度上取决于具体情况。
有没有一种避免将请求作为参数传递的方法?
render()
与调用render_to_response()
并使用强制使用RequestContext
参数的context_instance
参数相同。
class RequestContext
在此行中定义。
class RequestContext
的代码清单。
class RequestContext(Context):
"""
This subclass of template.Context automatically populates itself using
the processors defined in TEMPLATE_CONTEXT_PROCESSORS.
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,
use_l10n=None, use_tz=None):
Context.__init__(self, dict_, current_app=current_app,
use_l10n=use_l10n, use_tz=use_tz)
if processors is None:
processors = ()
else:
processors = tuple(processors)
updates = dict()
for processor in get_standard_processors() + processors:
updates.update(processor(request))
self.update(updates)
上一个问题不需要回答,如果你理解Django背后的代码是如何工作的。