Opened 17 years ago

Closed 17 years ago

#6321 closed (invalid)

Template processor names with no dots cause admin failure

Reported by: holdenweb Owned by: nobody
Component: Core (Management commands) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I set a local TEMPLATE_CONTEXT_PROCESSOR in my settings file as follows

TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"hw_context_processor")

[i.e. I put the hw_context_processor directly into views.py]. Everything worked fine until I tried the /admin/ pages, when I got a traceback:

Environment:

Request Method: GET
Request URL: http://holdenwe.user.openhosting.com/admin/
Django Version: 0.97-pre-SVN-6994
Python Version: 2.4.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.humanize',
 'registration',
 'mainapp']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')


Traceback:
File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in get_response
  82.                 response = callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/decorators.py" in _checklogin
  56.             return view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  39.         response = view_func(request, *args, **kwargs)
File "/usr/lib/python2.4/site-packages/django/contrib/admin/views/main.py" in index
  233.     return render_to_response('admin/index.html', {'title': _('Site administration')}, context_instance=template.RequestContext(request))
File "/usr/lib/python2.4/site-packages/django/template/context.py" in __init__
  102.         for processor in get_standard_processors() + processors:
File "/usr/lib/python2.4/site-packages/django/template/context.py" in get_standard_processors
  80.                 raise ImproperlyConfigured('Error importing request processor module %s: "%s"' % (module, e))

Exception Type: ImproperlyConfigured at /admin/
Exception Value: Error importing request processor module hw_context_processo: "No module named hw_context_processo"

The problem appears to be in /usr/lib/python2.4/site-packages/django/template/context.py, where the code in get_standard_processors assumes there will always be at least one dot in the context processor module names. This surmise was confirmed by putting the context processor in a separate module and altering its spec to "lib.hw_context_processor", when the error went away.

The code causing the problem reads:

  73.  processors = []
  74. for path in settings.TEMPLATE_CONTEXT_PROCESSORS:
  75. i = path.rfind('.')
  76. module, attr = path[:i], path[i+1:]
  77. try:
  78. mod = __import__(module, {}, {}, [attr])
  79. except ImportError, e:
  80. raise ImproperlyConfigured('Error importing request processor module %s: "%s"' % (module, e)) ...

Line 76 could be changed to

if i != -1:
    module, attr = "", path
else:
    <current line 76>

but the empty module name then causes problems in a subsequent call of import().

Since I am a Django noob, I am prepared to accept my coding style was far from perfect. So theAappropriate solution might just be to alter the documentation to tell site developers to always put context processors in separate modules, and check explicitly for that in get_standard_processors().

Hope this helps, it's my first Django ticket.

Change History (1)

comment:1 by Steve Holden <steve@…>, 17 years ago

Resolution: invalid
Status: newclosed

This ticket was based on a misunderstanding: since context processors must be callables, not modules, their names should clearly require at least one dot.

I've closed this ticket as invalid, which I hope is the correct action: sorry for the noise.

Note: See TracTickets for help on using tickets.
Back to Top