Skip to content

"Undefined template variable" During pytest Runs #134

@Dresdn

Description

@Dresdn

I'm trying to integrate django-cotton into a mature project, and I'm running into issues when default vars not being used during pytest runs.

Templates

cotton/button.html:

<c-vars type="button" classes="" />
<button type="{{ type }}" class="c-button {{ classes }}" {{ attrs }}>
  {{ slot }}
</button>

index.html:

<c-button classes="foo">Cotton Button</c-button>

Issue

When running the app, the component renders with type="button" as expected. However, running pytest, I get:

Failed: Undefined template variable 'type' in '/path/to/app/server/templates/cotton/button.html'

I've tried this on a simpler project (cloned the Mozilla Library Tutorial), and it works as expected, so it's my project. The challenge is that I'm not strong with the templating system, so any guidance on where to look would be great.

I appreciate any help and/or guidance!

Extra Info

Settings

Happy to provide more if helpful.

INSTALLED_APPS = [..., 'django_cotton']

TEMPLATES = [
    {
        'APP_DIRS': True,
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR.joinpath('server', 'templates'),
        ],
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.request',
                'server.apps.base.context_processors.export_vars',
                'server.apps.base.context_processors.meta',
            ],
        },
    },
]

pytest Stack Track

.venv/lib/python3.11/site-packages/django/template/base.py:880: in _resolve_lookup
    current = current[bit]
.venv/lib/python3.11/site-packages/django/template/context.py:83: in __getitem__
    raise KeyError(key)
E   KeyError: 'type'

During handling of the above exception, another exception occurred:
.venv/lib/python3.11/site-packages/django/template/base.py:886: in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(
E   AttributeError: type object 'Context' has no attribute 'type'

During handling of the above exception, another exception occurred:
.venv/lib/python3.11/site-packages/django/template/base.py:896: in _resolve_lookup
    current = current[int(bit)]
E   ValueError: invalid literal for int() with base 10: 'type'

During handling of the above exception, another exception occurred:
.venv/lib/python3.11/site-packages/django/template/base.py:715: in resolve
    obj = self.var.resolve(context)
.venv/lib/python3.11/site-packages/django/template/base.py:847: in resolve
    value = self._resolve_lookup(context)
.venv/lib/python3.11/site-packages/django/template/base.py:903: in _resolve_lookup
    raise VariableDoesNotExist(
E   django.template.base.VariableDoesNotExist: Failed lookup for key [type] in [{'True': True, 'False': False, 
'None': None}, {'True': True, 'False': False, 'None': None, 'csrf_token': <SimpleLazyObject: <function csrf.
<locals>._get_val at 0x129357e20>>, 'user': <Mock id='4984067536'>, 'perms': PermWrapper(<Mock id
='4984067536'>), 'LANGUAGES': (('en', 'English'),), 'LANGUAGE_CODE': 'en-us', 'LANGUAGE_BIDI': False, 
'MEDIA_URL': '/media/', 'messages': [], 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 
25, 'WARNING': 30, 'ERROR': 40}, 'request': <WSGIRequest: GET '/'>, 'DEVELOPMENT': True, 'DOMAIN_NAME': 
'domain.test', 'tenants_count': 0, 'first_additional_tenant': None, 'project_meta': {'NAME': 'Company', 'URL': 
'http://domain.test', 'DESCRIPTION': 'Some Text', 'IMAGE': 'https://www.domain.com/image.jpg', 'KEYWORDS': 
'neat, thing', 'CONTACT_EMAIL': '[email protected]', 'TITLE': 'Page Title'}, 'page_url': 'https:////', 'page_title': 
'', 'page_description': '', 'page_image': '', 'slot': 'Cotton Button', 'ctn_unprocessable_dynamic_attrs': set(), 
'attrs': 'classes="foo"', 'attrs_dict': {'classes': 'foo'}, 'classes': 'foo'}]

During handling of the above exception, another exception occurred:
tests/test_apps/test_index_view.py:24: in test_root_page
    response = index(request)
server/apps/main/views.py:24: in index
    return render(request, 'index.html')
.venv/lib/python3.11/site-packages/django/shortcuts.py:24: in render
    content = loader.render_to_string(template_name, context, request, using=using)
.venv/lib/python3.11/site-packages/django/template/loader.py:62: in render_to_string
    return template.render(context, request)
.venv/lib/python3.11/site-packages/django/template/backends/django.py:61: in render
    return self.template.render(context)
.venv/lib/python3.11/site-packages/django/template/base.py:175: in render
    return self._render(context)
.venv/lib/python3.11/site-packages/django/test/utils.py:112: in instrumented_test_render
    return self.nodelist.render(context)
.venv/lib/python3.11/site-packages/django/template/base.py:1005: in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
.venv/lib/python3.11/site-packages/django/template/base.py:1005: in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
.venv/lib/python3.11/site-packages/django/template/base.py:966: in render_annotated
    return self.render(context)
.venv/lib/python3.11/site-packages/django_cotton/templatetags/_component.py:97: in render
    return tpl.render(ctx)
.venv/lib/python3.11/site-packages/django/template/backends/django.py:61: in render
    return self.template.render(context)
.venv/lib/python3.11/site-packages/django/template/base.py:175: in render
    return self._render(context)
.venv/lib/python3.11/site-packages/django/test/utils.py:112: in instrumented_test_render
    return self.nodelist.render(context)
.venv/lib/python3.11/site-packages/django/template/base.py:1005: in render
    return SafeString("".join([node.render_annotated(context) for node in self]))
.venv/lib/python3.11/site-packages/django/template/base.py:1005: in <listcomp>
    return SafeString("".join([node.render_annotated(context) for node in self]))
.venv/lib/python3.11/site-packages/django/template/base.py:966: in render_annotated
    return self.render(context)
.venv/lib/python3.11/site-packages/django_cotton/templatetags/_vars_frame.py:43: in render
    c_vars[key] = value.resolve(context)
.venv/lib/python3.11/site-packages/django/template/base.py:723: in resolve
    return string_if_invalid % self.var
.venv/lib/python3.11/site-packages/pytest_django/plugin.py:703: in __mod__
    pytest.fail(msg)
E   Failed: Undefined template variable 'type' in '/path/to/app/server/templates/cotton/button.html'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions