在django 1.9中使用is_popup变量获取错误

时间:2016-01-14 19:17:42

标签: django django-admin

每当我想访问它时,我一直在管理员中收到错误,并且在我在uWSGI中部署它时崩溃了。这是错误:

DEBUG:django.template:Exception while resolving variable 'is_popup' in template 'admin/login.html'.
Traceback (most recent call last):
  File "/home/leandro/.envs/********/lib/python3.4/site-packages/django/template/base.py", line 879, in _resolve_lookup
    current = current[bit]
  File "/home/leandro/.envs/*******/lib/python3.4/site-packages/django/template/context.py", line 77, in __getitem__
    raise KeyError(key)
KeyError: 'is_popup'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/leandro/.envs/********/lib/python3.4/site-packages/django/template/base.py", line 885, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'RequestContext' has no attribute 'is_popup'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/leandro/.envs/********/lib/python3.4/site-packages/django/template/base.py", line 894, in _resolve_lookup
    current = current[int(bit)]
ValueError: invalid literal for int() with base 10: 'is_popup'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/leandro/.envs/******/lib/python3.4/site-packages/django/template/base.py", line 901, in _resolve_lookup
    (bit, current))  # missing attribute
django.template.base.VariableDoesNotExist: Failed lookup for key [is_popup] in "[{'False': False, 'None': None, 'True': True}, {'MEDIA_URL': '/media/', 'DEFAULT_MESSAGE_LEVELS': {'ERROR': 40, 'WARNING': 30, 'INFO': 20, 'SUCCESS': 25, 'DEBUG': 10}, 'LANGUAGE_CODE': 'es-AR', 'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7ff93002a8d0>, 'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x7ff92b377d08>>, 'LANGUAGE_BIDI': False, 'TIME_ZONE': 'ART', 'STATIC_URL': '/static/', 'user': <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7ff93002a828>>, 'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7ff92b324c18>, 'LANGUAGES': [('af', 'Afrikaans'), ('ar', 'Arabic'), ('ast', 'Asturian'), ('az', 'Azerbaijani'), ('bg', 'Bulgarian'), ('be', 'Belarusian'), ('bn', 'Bengali'), ('br', 'Breton'), ('bs', 'Bosnian'), ('ca', 'Catalan'), ('cs', 'Czech'), ('cy', 'Welsh'), ('da', 'Danish'), ('de', 'German'), ('el', 'Greek'), ('en', 'English'), ('en-au', 'Australian English'), ('en-gb', 'British English'), ('eo', 'Esperanto'), ('es', 'Spanish'), ('es-ar', 'Argentinian Spanish'), ('es-co', 'Colombian Spanish'), ('es-mx', 'Mexican Spanish'), ('es-ni', 'Nicaraguan Spanish'), ('es-ve', 'Venezuelan Spanish'), ('et', 'Estonian'), ('eu', 'Basque'), ('fa', 'Persian'), ('fi', 'Finnish'), ('fr', 'French'), ('fy', 'Frisian'), ('ga', 'Irish'), ('gd', 'Scottish Gaelic'), ('gl', 'Galician'), ('he', 'Hebrew'), ('hi', 'Hindi'), ('hr', 'Croatian'), ('hu', 'Hungarian'), ('ia', 'Interlingua'), ('id', 'Indonesian'), ('io', 'Ido'), ('is', 'Icelandic'), ('it', 'Italian'), ('ja', 'Japanese'), ('ka', 'Georgian'), ('kk', 'Kazakh'), ('km', 'Khmer'), ('kn', 'Kannada'), ('ko', 'Korean'), ('lb', 'Luxembourgish'), ('lt', 'Lithuanian'), ('lv', 'Latvian'), ('mk', 'Macedonian'), ('ml', 'Malayalam'), ('mn', 'Mongolian'), ('mr', 'Marathi'), ('my', 'Burmese'), ('nb', 'Norwegian Bokmal'), ('ne', 'Nepali'), ('nl', 'Dutch'), ('nn', 'Norwegian Nynorsk'), ('os', 'Ossetic'), ('pa', 'Punjabi'), ('pl', 'Polish'), ('pt', 'Portuguese'), ('pt-br', 'Brazilian Portuguese'), ('ro', 'Romanian'), ('ru', 'Russian'), ('sk', 'Slovak'), ('sl', 'Slovenian'), ('sq', 'Albanian'), ('sr', 'Serbian'), ('sr-latn', 'Serbian Latin'), ('sv', 'Swedish'), ('sw', 'Swahili'), ('ta', 'Tamil'), ('te', 'Telugu'), ('th', 'Thai'), ('tr', 'Turkish'), ('tt', 'Tatar'), ('udm', 'Udmurt'), ('uk', 'Ukrainian'), ('ur', 'Urdu'), ('vi', 'Vietnamese'), ('zh-hans', 'Simplified Chinese'), ('zh-hant', 'Traditional Chinese')]}, {}, {'next': '/admin/', 'LANGUAGE_BIDI': False, 'available_apps': [], 'title': 'Identificarse', 'site_title': <django.utils.functional.lazy.<locals>.__proxy__ object at 0x7ff9369bd390>, 'site': <Site: example.com>, 'LANGUAGE_CODE': 'es-AR', 'site_name': 'example.com', 'site_url': '/', 'form': <AdminAuthenticationForm bound=False, valid=Unknown, fields=(username;password)>, 'has_permission': False, 'site_header': <django.utils.functional.lazy.<locals>.__proxy__ object at 0x7ff9369bd438>, 'app_path': '/admin/login/?next=/admin/'}]"

我已经尝试禁用我注册的所有管理网站,但我一直收到此错误消息。有什么想法吗?

3 个答案:

答案 0 :(得分:21)

我也得到了相同的日志消息,全新安装了django 1.10.6。和日志记录打开。我不是这方面的专家,但在Django-users邮件列表上询问question后,我发现了这一点。

1)这是一个DEBUG消息,而不是“错误”。

正如您可以从Django logging documentation读取的那样,日志级别从最低优先级到最高优先级

  • DEBUG:用于调试目的的低级系统信息
  • 信息:一般系统信息
  • 警告:描述已发生的小问题的信息。
  • 错误:描述已发生的主要问题的信息。
  • 关键:描述已发生的严重问题的信息。

这意味着,实际上“很高兴知道”低级信息,如果出现故障您将记录,否则您无法找到原因。

2)它是一个缺少的模板变量。

如果你看一下例外的第一行,就说

Exception while resolving variable 'is_popup' in template 'admin/login.html'

'admin / index.html'扩展'base_site.html',它扩展了'base.html',它有以下行(靠近文件的开头):

<body class="{% if is_popup %}popup {% endif %}{% block bodyclass %}{% endblock %}"
  data-admin-utc-offset="{% now "Z" %}">

<!-- Container -->
<div id="container">

    {% if not is_popup %}
    <!-- Header -->
    <div id="header">
        <div id="branding">
        {% block branding %}{% endblock %}
        </div>

你可以从django logging documentation看到,缺少模板变量导致从Django 1.9开始的DEBUG级别日志消息。

3)将“undefined”用作“false”

是一种常见的模式

缺少模板变量的原因是django重用模板。在某些情况下,会定义一个模板变量'is_popup',而django会以不同的方式呈现页面。当没有'is_popup'这样的变量时,django将跳过相关的行。即undefinedfalse在模板文件中用于比较相同。当然,您可以编写代码来检查变量是否存在,但它只是额外的代码行。无论如何,Django将跳过未定义的变量(核心代码中的一些尝试..异常模式)。

4)您可以轻松地抑制这些异常

只需添加一个名为“django.template”的记录器,即可使日志级别为INFO。

'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
    'django.template': {
        'handlers': ['file'],
        'level': 'INFO',
        'propagate': True,
    },
},

答案 1 :(得分:0)

请参阅SESSION_COOKIE_DOMAIN必须与您的域名或父域

匹配
SESSION_COOKIE_DOMAIN
# maybe it help too
SESSION_COOKIE_SECURE

答案 2 :(得分:-4)

您应该将项目日志记录级别设置为不在debug.set中,以便信息可以正常工作。

    'loggers': {
    'django': {
        'handlers': ['file', 'console'],
        'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),

        'propagate': True,
    },
},

请参阅https://github.com/django/django/blob/cfda1fa3f8d95f0f4a369da9021dbd770e5fa44a/django/template/base.py#L911