Skip to content

Jinja2 template fails to render when it contains include statements #19490

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

Closed
mgades opened this issue May 14, 2025 · 3 comments · Fixed by #19570
Closed

Jinja2 template fails to render when it contains include statements #19490

mgades opened this issue May 14, 2025 · 3 comments · Fixed by #19570
Assignees
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Milestone

Comments

@mgades
Copy link

mgades commented May 14, 2025

Deployment Type

Self-hosted

NetBox Version

v.4.3.1

Python Version

3.10

Steps to Reproduce

  1. Create two Jinja2 template files ('router/main.j2' and 'common/base.j2'). Content could be empty or random text.
  2. Add a local Data Source (eg. named "netconfig") with the path from the above file locations.
  3. Add 'router/main.j2' as a Configuration Template and attach it to a device (eg. RTR1).
  4. In the 'router/main.j2' file add the following: {%- include 'common/base.j2' %}
  5. Sync the "netconfig" Data Source to update the changes in the file.
  6. Go the the RTR1 device and click the Render Config tab.

Expected Behavior

I would expect the content from 'common/base.j2' to be included in the output on the screen.

It worked fine in v4.2.8 (before upgrading to 4.3.0 and 4.3.1).

Observed Behavior

The complete exception is provided below:

<class 'TypeError'>

no loader for this environment specified

Python version: 3.10.12
NetBox version: 4.3.1
Plugins: None installed

@mgades mgades added type: bug A confirmed report of unexpected behavior in the application status: needs triage This issue is awaiting triage by a maintainer labels May 14, 2025
@dillon-conner
Copy link

dillon-conner commented May 14, 2025

Also having this issue, same symptoms. Have a github data source for config files with includes/extends.

Issue started with 4.3 upgrade, same template and data source. Persists in 4.3.1 release as of this morning.
Config template render fails with:

netbox-1               | Internal Server Error: /dcim/devices/3/render-config/
netbox-1               | Traceback (most recent call last):
netbox-1               |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
netbox-1               |     response = get_response(request)
netbox-1               |                ^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
netbox-1               |     response = wrapped_callback(request, *callback_args, **callback_kwargs)
netbox-1               |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/views/generic/base.py", line 104, in view
netbox-1               |     return self.dispatch(request, *args, **kwargs)
netbox-1               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/netbox/netbox/views/generic/base.py", line 26, in dispatch
netbox-1               |     return super().dispatch(request, *args, **kwargs)
netbox-1               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/netbox/utilities/views.py", line 125, in dispatch
netbox-1               |     return super().dispatch(request, *args, **kwargs)
netbox-1               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/netbox/utilities/views.py", line 39, in dispatch
netbox-1               |     return super().dispatch(request, *args, **kwargs)
netbox-1               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/venv/lib/python3.12/site-packages/django/views/generic/base.py", line 143, in dispatch
netbox-1               |     return handler(request, *args, **kwargs)
netbox-1               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/netbox/extras/views.py", line 936, in get
netbox-1               |     context = self.get_extra_context(request, instance)
netbox-1               |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/netbox/extras/views.py", line 972, in get_extra_context
netbox-1               |     rendered_config = config_template.render(context=context_data)
netbox-1               |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/netbox/extras/models/mixins.py", line 134, in render
netbox-1               |     output = render_jinja2(self.template_code, context, env_params)
netbox-1               |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/netbox/utilities/jinja2.py", line 59, in render_jinja2
netbox-1               |     return environment.from_string(source=template_code).render(**context)
netbox-1               |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
netbox-1               |   File "/opt/netbox/venv/lib/python3.12/site-packages/jinja2/environment.py", line 1295, in render
netbox-1               |     self.environment.handle_exception()
netbox-1               |   File "/opt/netbox/venv/lib/python3.12/site-packages/jinja2/environment.py", line 942, in handle_exception
netbox-1               |     raise rewrite_traceback_stack(source=source)
netbox-1               |   File "<template>", line 1, in top-level template code
netbox-1               | TypeError: no loader for this environment specified

Image

@jnovinger jnovinger added status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation severity: medium Results in substantial degraded or broken functionality for specfic workflows and removed status: needs triage This issue is awaiting triage by a maintainer labels May 16, 2025
@jnovinger
Copy link
Member

Did a little debugging on this, the offending change appears to be after the v4.2.8/v4.2.9 tags and before the v4.3.0-beta1 tag. List of changes: v4.3.0-beta1...v4.2.8

@jnovinger jnovinger self-assigned this May 16, 2025
@jnovinger jnovinger added status: accepted This issue has been accepted for implementation and removed status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels May 16, 2025
@mgades
Copy link
Author

mgades commented May 23, 2025

Did a little debugging on this, the offending change appears to be after the v4.2.8/v4.2.9 tags and before the v4.3.0-beta1 tag. List of changes: v4.3.0-beta1...v4.2.8

Thanks @jnovinger - please let me know if you need further info from me...

jnovinger added a commit that referenced this issue May 23, 2025
…plates

The ability to render nested templates was accidentally removed with the
implementation of #17653, which normalized the behavior of various Jinja2
template rendering actions.

This fix restores that behavior while retaining the normalized behavior.
This fix also includes regression tests to ensure this behavior is not
removed accidentally again in the future.
bctiemann added a commit that referenced this issue May 28, 2025
…fails-with-empty-include

Fixes #19490: restores nesting behavior of DataSource-based ConfigTemplate
@jeremystretch jeremystretch added this to the v4.3.2 milestone May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
severity: medium Results in substantial degraded or broken functionality for specfic workflows status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants