Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyError being thrown after logging in to Django admin

I'm getting a pretty nasty KeyError after logging into the Django admin. It is unable to find the key "user". If it helps, I have a model named "User".

How do I fix this? Could something be wrong with my configuration? I am using the default admin configuration mentioned in the 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

Here is part of my settings.py:

# 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',
)

2 Answers

Try adding this to your TEMPLATE_CONTEXT_PROCESSORS :

django.contrib.auth.context_processors.auth

It makes the user object available to templates through the RequestContext: https://docs.djangoproject.com/en/dev/ref/templates/api/#django-contrib-auth-context-processors-auth

like image 191
jdi Avatar answered Nov 25 '25 12:11

jdi


Those on Django 1.8 and above: the TEMPLATE_CONTEXT_PROCESSORS settings has been deprecated due to the new "multiple template backends" work landing. If you are using the new TEMPLATES setting, you must instead set your context processors on the engine you require them to be used with. For this example, it means setting it like so:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                '..etc..'
            ],
        },
    },
]

I have deliberately left off the other options available in an engine dict to make it more clear.

like image 30
Darian Moody Avatar answered Nov 25 '25 12:11

Darian Moody



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!