Django 1.10 KeyError at / admin /

时间:2016-12-21 06:03:47

标签: python django

我有一个用Django 1.7编写的Django应用程序,我正在移植到Docker应用程序。我在容器中使用了Django 1.10。

当我浏览到/ admin / URI时,我得到以下异常:

KeyError at /admin/

u'user'

Request Method:     GET
Request URL:        http://10.3.101.206:9000/admin/
Django Version:     1.10
Exception Type:     KeyError
Exception Value:    

u'user'

Exception Location:     /usr/local/lib/python2.7/site-packages/django/template/context.py in __getitem__, line 75
Python Executable:  /usr/local/bin/python
Python Version:     2.7.12
Python Path:    

['/var/www/django/labmgr',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages']

Server time:    Tue, 20 Dec 2016 21:43:21 -0800

Error during template rendering

In template /usr/local/lib/python2.7/site-packages/django/contrib/admin/templates/admin/index.html, error at line 58
user

48  {% endif %}
49  </div>
50  {% endblock %}
51  
52  {% block sidebar %}
53  <div id="content-related">
54      <div class="module" id="recent-actions-module">
55          <h2>{% trans 'Recent actions' %}</h2>
56          <h3>{% trans 'My actions' %}</h3>
57              {% load log %}
58              {% get_admin_log 10 as admin_log for_user user %}
59              {% if not admin_log %}
60              <p>{% trans 'None available' %}</p>
61              {% else %}
62              <ul class="actionlist">
63              {% for entry in admin_log %}
64              <li class="{% if entry.is_addition %}addlink{% endif %}{% if entry.is_change %}changelink{% endif %}{% if entry.is_deletion %}deletelink{% endif %}">
65                  {% if entry.is_deletion or not entry.get_admin_url %}
66                      {{ entry.object_repr }}
67                  {% else %}
68                      <a href="{{ entry.get_admin_url }}">{{ entry.object_repr }}</a>

错误似乎出现在Django代码中,而不是我的代码中,因此我不确定问题是什么或如何修复它。

1 个答案:

答案 0 :(得分:2)

KeyError暗示无法找到user上下文变量。该变量仅可通过django.contrib.auth.context_processors.auth获得,默认情况下通常包含该变量。

您可以按照here所述的上下文处理器中包含它:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            # insert your TEMPLATE_DIRS here
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                # ...list other context processors you will need...
            ],
        },
    },
]