Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error with is_popup variable in django 1.9

I keep getting an error in the admin whenever I want to access it and it crashes the moment I deploy it in uWSGI. Here's the error:

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/'}]"

I've tried disabling all the admin sites I registered and I keep getting this error. Any ideas?

like image 676
Leandro Poblet Avatar asked Jan 14 '16 19:01

Leandro Poblet


1 Answers

I was getting the same log message also, with fresh install of django 1.10.6. and logging turned on. I am not an expert on this, but here is what I found out, after asking a question on the Django-users mailing list.

1) It is a DEBUG-message, not an "Error".

As you can read from the Django logging documentation, the log levels are from the lowest priority to the highest priority

  • DEBUG: Low level system information for debugging purposes
  • INFO: General system information
  • WARNING: Information describing a minor problem that has occurred.
  • ERROR: Information describing a major problem that has occurred.
  • CRITICAL: Information describing a critical problem that has occurred.

This means, it is actually "nice to know" low level information, which you will log if something fails, and you can not find a reason otherwise.

2) It is about a missing template variable.

If you look at the first line of the exception, it says

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

The 'admin/index.html' extends 'base_site.html', which extends 'base.html', which has following lines (near the beginning of the file):

<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>

You can see from the django logging documentation that missing template variables cause DEBUG level log messages starting from Django 1.9.

3) It is a common pattern to use "undefined" as "false"

The reason for the missing template variable is that django reuses templates. And in some cases, there will be a template variable 'is_popup' defined, and django will render the page differently. When there is no such variable as 'is_popup', django will skip the associated lines. I.e. undefined and false are identical for comparison purposes inside the template file. Of course you could write code to check if a variable exists, but it would just be extra line of code. Django will skip the undefined variable (some try .. exception pattern inside the core code) anyway.

4) You may suppress these exceptions easily

Just include a logger with name "django.template" to have the log level INFO only.

'loggers': {
    'django': {
        'handlers': ['file'],
        'level': 'DEBUG',
        'propagate': True,
    },
    'django.template': {
        'handlers': ['file'],
        'level': 'INFO',
        'propagate': True,
    },
},
like image 148
np8 Avatar answered Oct 12 '22 01:10

np8