Django管理者にログインした後、かなり厄介なKeyErrorが発生しました。キー "user"を見つけることができません。それが役に立ったら、私は "User"というモデルを持っています。Django管理者にログインした後でKeyErrorがスローされました
これを修正するにはどうすればよいですか?私の設定に何か問題がありますか?私はDjango tutorialsに記載されているデフォルトの管理設定を使用しています。ここで
[07/Feb/2012 19:04:52] "GET /web/admin/ HTTP/1.1" 500 1865
http://localhost:8000/web/admin/
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 111, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py", line 214, in wrapper
return self.admin_view(view, cacheable)(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/utils/decorators.py", line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py", line 197, in inner
return view(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/sites.py", line 382, in index
context_instance=context_instance
File "/usr/local/lib/python2.6/dist-packages/django/shortcuts/__init__.py", line 20, in render_to_response
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader.py", line 188, in render_to_string
return t.render(context_instance)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 123, in render
return self._render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 117, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 757, in render_node
return node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py", line 127, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 117, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 757, in render_node
return node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py", line 127, in render
return compiled_parent._render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 117, in _render
return self.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 757, in render_node
return node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/loader_tags.py", line 64, in render
result = block.nodelist.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 744, in render
bits.append(self.render_node(node, context))
File "/usr/local/lib/python2.6/dist-packages/django/template/base.py", line 757, in render_node
return node.render(context)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/admin/templatetags/log.py", line 19, in render
user_id = context[self.user].id
File "/usr/local/lib/python2.6/dist-packages/django/template/context.py", line 55, in __getitem__
raise KeyError(key)
KeyError: u'user'
[07/Feb/2012 19:06:28] "GET /web/admin/ HTTP/1.1" 500 1865
私のsettings.pyの一部である:あなたのTEMPLATE_CONTEXT_PROCESSORSにこれを追加すること
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.request',
'django.contrib.messages.context_processors.messages',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'web.exception.Middleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
)
TEMPLATE_DIRS = (
BASE('web/templates')
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sites',
'django.contrib.sessions',
'django.contrib.messages',
...
'django_nose',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
あなたは 'settings.py'を投稿できますか?コンテクストプロセッサが存在しないように見えるかもしれません。通常、レンダリングして失敗するテンプレートに対してユーザオブジェクトを利用できるようになります。 – jdi
@jdi 'settings.py'の一部を追加しました –