Page error on config render with empty output #11176

Closed
opened 2025-12-29 21:41:24 +01:00 by adam · 4 comments
Owner

Originally created by @Omripresent on GitHub (May 15, 2025).

Originally assigned to: @Omripresent on GitHub.

Deployment Type

Self-hosted

NetBox Version

v4.2.6, v4.3.1

Python Version

3.12

Steps to Reproduce

  1. Create a config template with the following content
    {# empty template test #}
    
  2. Create a device and attach the template created in step 1
  3. Open the device config render tab

Expected Behavior

A config render page with no output

Observed Behavior

Django template render error due to attempting to translate a None assigned variable

<class 'AttributeError'>

'NoneType' object has no attribute 'replace'

Python version: 3.12.5
NetBox version: 4.2.6
Plugins: 
  netbox_attachments: 7.2.0
  netbox_dns: 1.2.1
  netbox_inventory: 2.3.0
Originally created by @Omripresent on GitHub (May 15, 2025). Originally assigned to: @Omripresent on GitHub. ### Deployment Type Self-hosted ### NetBox Version v4.2.6, v4.3.1 ### Python Version 3.12 ### Steps to Reproduce 1. Create a config template with the following content ``` {# empty template test #} ``` 1. Create a device and attach the template created in step 1 1. Open the device config render tab ### Expected Behavior A config render page with no output ### Observed Behavior Django template render error due to attempting to translate a `None` assigned variable ``` <class 'AttributeError'> 'NoneType' object has no attribute 'replace' Python version: 3.12.5 NetBox version: 4.2.6 Plugins: netbox_attachments: 7.2.0 netbox_dns: 1.2.1 netbox_inventory: 2.3.0 ```
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 21:41:24 +01:00
adam closed this issue 2025-12-29 21:41:24 +01:00
Author
Owner

@Omripresent commented on GitHub (May 15, 2025):

I found the issue to be related to this specific section:

cf7ab43f39/netbox/templates/extras/object_render_config.html (L65-L70)

When the template renders successfully without error but the output sum is an empty string we hit this condition when the view passes the error_message variable with a None value here:

cf7ab43f39/netbox/extras/views.py (L968-L982)

There are a few possible ways to correct this error:

  1. Change the template to account for the condition of no error empty template
  2. Update the get_extra_context function to initialize error_message to an empty string

Both should be easy to implement.

@Omripresent commented on GitHub (May 15, 2025): I found the issue to be related to this specific section: https://github.com/netbox-community/netbox/blob/cf7ab43f397172f71b2068eb5347953c0d408ce9/netbox/templates/extras/object_render_config.html#L65-L70 When the template renders successfully without error but the output sum is an empty string we hit this condition when the view passes the `error_message` variable with a `None` value here: https://github.com/netbox-community/netbox/blob/cf7ab43f397172f71b2068eb5347953c0d408ce9/netbox/extras/views.py#L968-L982 There are a few possible ways to correct this error: 1. Change the template to account for the condition of no error empty template 2. Update the `get_extra_context` function to initialize error_message to an empty string Both should be easy to implement.
Author
Owner

@jnovinger commented on GitHub (May 16, 2025):

@Omripresent , would you like to work on this?

I think the second approach is an incomplete solution the template code is expected one of two branches and this is really just showing there's a third--like you said, where the template renders correctly, but the template just checks for falsey-ness.

What if the template checked to make sure rendered_config was not None on line 53?

@jnovinger commented on GitHub (May 16, 2025): @Omripresent , would you like to work on this? I think the second approach is an incomplete solution the template code is expected one of two branches and this is really just showing there's a third--like you said, where the template renders correctly, but the template just checks for falsey-ness. What if the template checked to make sure `rendered_config` was not None on line 53?
Author
Owner

@Omripresent commented on GitHub (May 16, 2025):

I've already tested a few options so I can take this on.
I was thinking since the error_message == None state is the cause of issue we can add {% elif error_message %} at line 66 and an additional {% else %} statement to cover the blank output state.

        {% elif error_message %}
          <div class="alert alert-warning">
            <h4 class="alert-title mb-1">{% trans "Error rendering template" %}</h4>
            {% trans error_message is none %}
          </div>
        {% else %}
          <div class="alert alert-warning">
            <h4 class="alert-title mb-1">{% trans "Template output is empty" %}</h4>
          </div>
        {% endif %}

This way there shouldn't be a case where error_message will be invoked. It also allows for a more verbose information about the state of the render compared to a generic "Rendered Config" header without any content.

@Omripresent commented on GitHub (May 16, 2025): I've already tested a few options so I can take this on. I was thinking since the `error_message == None` state is the cause of issue we can add `{% elif error_message %}` at line 66 and an additional `{% else %}` statement to cover the blank output state. ```jinja2 {% elif error_message %} <div class="alert alert-warning"> <h4 class="alert-title mb-1">{% trans "Error rendering template" %}</h4> {% trans error_message is none %} </div> {% else %} <div class="alert alert-warning"> <h4 class="alert-title mb-1">{% trans "Template output is empty" %}</h4> </div> {% endif %} ``` This way there shouldn't be a case where `error_message` will be invoked. It also allows for a more verbose information about the state of the render compared to a generic "Rendered Config" header without any content.
Author
Owner

@jnovinger commented on GitHub (May 16, 2025):

Sounds like a plan. As I think about it more, I think we should probably also initialize error_message to an empty string like you suggest.

@jnovinger commented on GitHub (May 16, 2025): Sounds like a plan. As I think about it more, I think we should probably **also** initialize `error_message` to an empty string like you suggest.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#11176