33👍
Most of the apps use render_to_response
since it was the default recommended option since the very beginning until Django 1.3. The reason for the coexistence of both is historical, deprecating render_to_response
will force a lot of code to be rewritten, which is not polite in minor releases. However in this django-developer thread they say it would be possible to include in the deprecation timeline for 2.0.
Here’s the quote by Russell Keith-Magee, one of Django’s core developers. Keith-Magee answers a question posted by Jacob Kaplan-Moss, another Django contributer who raises the question of deprecating render_to_response
:
I think we should deprecate render_to_response() in favor of render().
render_to_response() is just render(request=None, …), right? Any
reason to keep both around? There’s no particular reason to keep both around, other than the code churn that deprecation would entail.
And Keith-Magee answers:
This is something that I have no problem deprecating on the 2.0
schedule, but migrating every use of render_to_response() over the
next 18 months/2 releases seems like an extreme measure to enforce on
the entire user base when maintaining render_to_response() doesn’t
take any real effort.
Nobody’s been discussing that deprecation, but I guess the answer for your question is: There’s no technical reason, it’s just their intent not to force an update on all the codebase in a minor (at least no major) release.
8👍
Too long; didn’t read
When context processors are applied
When you use
RequestContext
, the variables you supply directly are added first, followed any variables supplied by context processors. This means that a context processor may overwrite a variable you’ve supplied, so take care to avoid variable names which overlap with those supplied by your context processors.
Let’s see first how the methods render_to_response
and render
are defined.
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)
Isn’t there a better way to enforce RequestContext as the default?
Note in the section Subclassing Context: RequestContext
If you’re using Django’s
render_to_response()
shortcut to populate a template with the contents of a dictionary, your template will be passed aContext
instance by default (not aRequestContext
)
From the code above the method render_to_response
calls the method loader.render_to_string
where context_instance
parameter is checked in this line.
Code listing for the method 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))
# Add the dictionary to the context stack, ensuring it gets removed again
# to keep the context_instance in the same state it started in.
with context_instance.push(dictionary):
return t.render(context_instance)
Can’t we do simple to use decorator with render
?
We can write decorator for this, but your question is subjective. If it’s easy to use or not is hard to tell. It very much depends
Is there a way to avoid passing request as an argument?
render()
is the same as a call torender_to_response()
with acontext_instance
argument that forces the use of aRequestContext
.
class RequestContext
is defined in this line.
Code listing for the 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)
Last question doesn’t need an answer, if you understand how the code behind Django actually works.
- [Django]-Detect whether Celery is Available/Running
- [Django]-Django : Table doesn't exist
- [Django]-How to remove models from django?