mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #925 -- Added TEMPLATE_CONTEXT_PROCESSORS, which lets you specify processesors for DjangoContext. Thanks, Luke Plant
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1773 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -2,11 +2,34 @@
|
||||
# of MVC. In other words, these functions/classes introduce controlled coupling
|
||||
# for convenience's sake.
|
||||
|
||||
from django.core.exceptions import Http404, ObjectDoesNotExist
|
||||
from django.core.exceptions import Http404, ImproperlyConfigured, ObjectDoesNotExist
|
||||
from django.core.template import Context, loader
|
||||
from django.conf.settings import DEBUG, INTERNAL_IPS
|
||||
from django.conf.settings import TEMPLATE_CONTEXT_PROCESSORS
|
||||
from django.utils.httpwrappers import HttpResponse
|
||||
|
||||
_standard_context_processors = None
|
||||
|
||||
# This is a function rather than module-level procedural code because we only
|
||||
# want it to execute if somebody uses DjangoContext.
|
||||
def get_standard_processors():
|
||||
global _standard_context_processors
|
||||
if _standard_context_processors is None:
|
||||
processors = []
|
||||
for path in TEMPLATE_CONTEXT_PROCESSORS:
|
||||
i = path.rfind('.')
|
||||
module, attr = path[:i], path[i+1:]
|
||||
try:
|
||||
mod = __import__(module, '', '', [attr])
|
||||
except ImportError, e:
|
||||
raise ImproperlyConfigured, 'Error importing request processor module %s: "%s"' % (module, e)
|
||||
try:
|
||||
func = getattr(mod, attr)
|
||||
except AttributeError:
|
||||
raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable request processor' % (module, attr)
|
||||
processors.append(func)
|
||||
_standard_context_processors = tuple(processors)
|
||||
return _standard_context_processors
|
||||
|
||||
def render_to_response(*args, **kwargs):
|
||||
return HttpResponse(loader.render_to_string(*args, **kwargs))
|
||||
load_and_render = render_to_response # For backwards compatibility.
|
||||
@@ -25,24 +48,19 @@ def get_list_or_404(mod, **kwargs):
|
||||
|
||||
class DjangoContext(Context):
|
||||
"""
|
||||
This subclass of template.Context automatically populates 'user' and
|
||||
'messages' in the 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):
|
||||
def __init__(self, request, dict=None, processors=None):
|
||||
Context.__init__(self, dict)
|
||||
self['user'] = request.user
|
||||
self['messages'] = request.user.get_and_delete_messages()
|
||||
self['perms'] = PermWrapper(request.user)
|
||||
from django.conf import settings
|
||||
self['LANGUAGES'] = settings.LANGUAGES
|
||||
if hasattr(request, 'LANGUAGE_CODE'):
|
||||
self['LANGUAGE_CODE'] = request.LANGUAGE_CODE
|
||||
if processors is None:
|
||||
processors = ()
|
||||
else:
|
||||
self['LANGUAGE_CODE'] = settings.LANGUAGE_CODE
|
||||
if DEBUG and request.META.get('REMOTE_ADDR') in INTERNAL_IPS:
|
||||
self['debug'] = True
|
||||
from django.core import db
|
||||
self['sql_queries'] = db.db.queries
|
||||
processors = tuple(processors)
|
||||
for processor in get_standard_processors() + processors:
|
||||
self.update(processor(request))
|
||||
|
||||
# PermWrapper and PermLookupDict proxy the permissions system into objects that
|
||||
# the template system can understand.
|
||||
|
||||
Reference in New Issue
Block a user