Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

django.template.base.VariableDoesNotExist: Failed lookup for key [script_url] #1159

Open
copyNdpaste opened this issue Jan 28, 2024 · 3 comments

Comments

@copyNdpaste
Copy link

copyNdpaste commented Jan 28, 2024

28/Jan/2024 05:55:15,013 base.py:_resolve_lookup:922 [DEBUG] Exception while resolving variable 'script_url' in template 'drf_spectacular/swagger_ui.html'.
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/template/base.py", line 875, in _resolve_lookup
    current = current[bit]
  File "/usr/local/lib/python3.9/site-packages/django/template/context.py", line 83, in __getitem__
    raise KeyError(key)
KeyError: 'script_url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/template/base.py", line 881, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(
AttributeError: type object 'RequestContext' has no attribute 'script_url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/template/base.py", line 891, in _resolve_lookup
    current = current[int(bit)]
ValueError: invalid literal for int() with base 10: 'script_url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/template/base.py", line 898, in _resolve_lookup
    raise VariableDoesNotExist(
django.template.base.VariableDoesNotExist: Failed lookup for key [script_url] in [{'True': True, 'False': False, 'None': None}, {'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0xffff73cf5040>>, 'request': <rest_framework.request.Request: GET '/api/docs'>, 'user': <django.contrib.auth.models.AnonymousUser object at 0xffff73ce2e80>, 'perms': PermWrapper(<django.contrib.auth.models.AnonymousUser object at 0xffff73ce2e80>), 'messages': <FallbackStorage: request=<WSGIRequest: GET '/api/docs'>>, 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}}, {}, {'title': 'API', 'swagger_ui_css': 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@latest/swagger-ui.css', 'swagger_ui_bundle': 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@latest/swagger-ui-bundle.js', 'swagger_ui_standalone': 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@latest/swagger-ui-standalone-preset.js', 'favicon_href': 'https://cdn.jsdelivr.net/npm/swagger-ui-dist@latest/favicon-32x32.png', 'schema_url': '/api/schema', 'settings': '{\n  "deepLinking": true\n}', 'oauth2_config': '{}', 'template_name_js': 'drf_spectacular/swagger_ui.js', 'csrf_header_name': 'X-CSRFTOKEN', 'schema_auth_names': '[]'}, {'block': <Block Node: body. Contents: [<TextNode: '\n    <div id="swagger-ui"'>, <Variable Node: swagger_ui_bundle>, <TextNode: '"></script>\n    <script s'>, <Variable Node: swagger_ui_standalone>, <TextNode: '"></script>\n    '>, <IfNode>, <TextNode: '\n    '>]>}]

settings.py

INSTALLED_APPS = [
    "drf_spectacular",

]
REST_FRAMEWORK = {
    "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}

urls.py

urlpatterns = [
    path(
        "api/schema",
        SpectacularAPIView.as_view(),
        name="schema",
    ),
    path(
        "api/docs",
        SpectacularSwaggerView.as_view(url_name="schema"),
        name="swagger-ui",
    ),
]

why VariableDoesNotExist error is raised?

@copyNdpaste
Copy link
Author

SERVER_ENV=local ./manage.py spectacular --file schema.yaml --validate
->
Schema generation summary:
Warnings: 1 (1 unique)
Errors: 0 (0 unique)

@tfranzel
Copy link
Owner

Hi, this is highly unusual. I'm not even sure how it is possible that line 22 produces an error. By definition the if is safe even if the variable is not existing, in which case false is assumed.

{% if script_url %}
<script src="{{ script_url }}"></script>
{% else %}
<script>
{% include template_name_js %}
</script>

  • Are you sure nothing else is broken on your side, since this works flawlessly for a lot of people and we haven't changed anything here for quite a while.
  • Do regular requests work for you?
  • Which version of Django/DRF/spectacular are you using?

@BramEsposito
Copy link
Contributor

@copyNdpaste do you have Django Debug Toolbar installed? I run into these errors when the Toolbar is visible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants