Error in export template silently causes the default export to be used instead #5654

Closed
opened 2025-12-29 19:30:42 +01:00 by adam · 3 comments
Owner

Originally created by @candlerb on GitHub (Nov 12, 2021).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.0.9

Python version

3.8

Steps to Reproduce

Create a new export template:

  • Content Type: dcim | cable
  • Name: Cables for CSV re-import
  • Template as follows:
side_a_device,side_a_type,side_a_name,side_b_device,side_b_type,side_b_name,type,status,label,color,length,length_unit
{%- for c in queryset %}
{%- set lenitems = c.length.split(' ') %}
{{ c.termination_a.device.name }},
{{ c.termination_a_type.app_labeled_name }},{{ c.termination_a.name }},{{ c.termination_b.device.name }},
{{ c.termination_b_type.app_labeled_name }},{{ c.termination_b.name }},{{ c.type }},{{ c.status }},{{ c.label }},{{ lenitems[0] }},{{ lenitems[1] }}
{%- endfor %}

Now browse to cables, select a filter which limits them to a single device.

Click on Export > Cables for CSV Import

image

This links to dcim/cables/?q=&device_id=952&type=&status=&color=&export=Cables%20for%20CSV%20re-import

Expected Behavior

The downloaded CSV file would be formatted in accordance with the given template

OR: if the template is invalid, that an error would be reported.

Observed Behavior

The file netbox_cables.csv which is generated uses the default CSV formatting, ignoring the selected export template:

ID,Label,Side A,Termination A,Side B,Termination B,Status,Type,Length,Color,Tags
1791,,ldex-r17-sw1,PSU,PDU-R17-1,OUT24,Connected,Power,,,
1808,,ldex-r17-sw1,Gi1/0/24,mod-rj45-r17-r18-1-4,F1,Connected,CAT5e,50.00 Centimeters,#ffeb3b,
... etc
Originally created by @candlerb on GitHub (Nov 12, 2021). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.0.9 ### Python version 3.8 ### Steps to Reproduce Create a new export template: * Content Type: `dcim | cable` * Name: `Cables for CSV re-import` * Template as follows: ``` side_a_device,side_a_type,side_a_name,side_b_device,side_b_type,side_b_name,type,status,label,color,length,length_unit {%- for c in queryset %} {%- set lenitems = c.length.split(' ') %} {{ c.termination_a.device.name }}, {{ c.termination_a_type.app_labeled_name }},{{ c.termination_a.name }},{{ c.termination_b.device.name }}, {{ c.termination_b_type.app_labeled_name }},{{ c.termination_b.name }},{{ c.type }},{{ c.status }},{{ c.label }},{{ lenitems[0] }},{{ lenitems[1] }} {%- endfor %} ``` Now browse to cables, select a filter which limits them to a single device. Click on `Export > Cables for CSV Import` ![image](https://user-images.githubusercontent.com/44789/141460998-70abed08-7fe7-44bb-88ac-76acf74b05e8.png) This links to `dcim/cables/?q=&device_id=952&type=&status=&color=&export=Cables%20for%20CSV%20re-import` ### Expected Behavior The downloaded CSV file would be formatted in accordance with the given template OR: if the template is invalid, that an error would be reported. ### Observed Behavior The file `netbox_cables.csv` which is generated uses the default CSV formatting, ignoring the selected export template: ``` ID,Label,Side A,Termination A,Side B,Termination B,Status,Type,Length,Color,Tags 1791,,ldex-r17-sw1,PSU,PDU-R17-1,OUT24,Connected,Power,,, 1808,,ldex-r17-sw1,Gi1/0/24,mod-rj45-r17-r18-1-4,F1,Connected,CAT5e,50.00 Centimeters,#ffeb3b, ... etc ```
adam added the type: bugstatus: accepted labels 2025-12-29 19:30:42 +01:00
adam closed this issue 2025-12-29 19:30:42 +01:00
Author
Owner

@candlerb commented on GitHub (Nov 12, 2021):

If I change the template body to just

Hello World

then (1) the downloaded file is called netbox_cables instead of netbox_cables.csv, and (2) it has the selected content.

If I change the template body to

{%- for c in queryset %}
{{ c.id }}
{%- endfor %}

then netbox_cables contains a list of IDs. So I believe that I had some error in the original template, but this error was silently ignored and caused the default rendering to be used instead. If so, this isn't very helpful when trying to debug a template.

@candlerb commented on GitHub (Nov 12, 2021): If I change the template body to just ``` Hello World ``` then (1) the downloaded file is called `netbox_cables` instead of `netbox_cables.csv`, and (2) it has the selected content. If I change the template body to ``` {%- for c in queryset %} {{ c.id }} {%- endfor %} ``` then `netbox_cables` contains a list of IDs. So I believe that I had some error in the original template, but this error was silently ignored and caused the default rendering to be used instead. If so, this isn't very helpful when trying to debug a template.
Author
Owner

@candlerb commented on GitHub (Nov 12, 2021):

Definitely the export template was bad. FWIW here's the debugged version:

side_a_device,side_a_type,side_a_name,side_b_device,side_b_type,side_b_name,type,status,label,color,length,length_unit
{%- for c in queryset %}
{%- if c.termination_a_type.app_label != 'circuits' and c.termination_b_type.app_label != 'circuits' %}
{{ c.termination_a.device.name }},{{ c.termination_a_type.app_labeled_name|replace(" | ",".")|replace(" ","") }},{{ c.termination_a.name }},{{ c.termination_b.device.name }},{{ c.termination_b_type.app_labeled_name|replace(" | ",".")|replace(" ","") }},{{ c.termination_b.name }},{{ c.type }},{{ c.status }},"{{ c.label }}",{{c.color}},{{ c.length|replace("None","") }},{{ c.length_unit }}
{%- endif %}
{%- endfor %}

But the point still stands: I think that broken export templates should generate errors when you try to use them.

@candlerb commented on GitHub (Nov 12, 2021): Definitely the export template was bad. FWIW here's the debugged version: ``` side_a_device,side_a_type,side_a_name,side_b_device,side_b_type,side_b_name,type,status,label,color,length,length_unit {%- for c in queryset %} {%- if c.termination_a_type.app_label != 'circuits' and c.termination_b_type.app_label != 'circuits' %} {{ c.termination_a.device.name }},{{ c.termination_a_type.app_labeled_name|replace(" | ",".")|replace(" ","") }},{{ c.termination_a.name }},{{ c.termination_b.device.name }},{{ c.termination_b_type.app_labeled_name|replace(" | ",".")|replace(" ","") }},{{ c.termination_b.name }},{{ c.type }},{{ c.status }},"{{ c.label }}",{{c.color}},{{ c.length|replace("None","") }},{{ c.length_unit }} {%- endif %} {%- endfor %} ``` But the point still stands: I think that broken export templates should generate errors when you try to use them.
Author
Owner

@candlerb commented on GitHub (Nov 12, 2021):

This might also be a browser-related issue. I'm using Chrome 95 under macOS 10.14.6.

I've just discovered that after the download has completed, if I switch to the "Filters" tab, change the filter, and switch back to the "Records" tab, I then see an error message:

image

However, the download did still complete (using the default export format)

@candlerb commented on GitHub (Nov 12, 2021): This might also be a browser-related issue. I'm using Chrome 95 under macOS 10.14.6. I've just discovered that after the download has completed, if I switch to the "Filters" tab, change the filter, and switch back to the "Records" tab, I *then* see an error message: ![image](https://user-images.githubusercontent.com/44789/141468681-0ca1d98f-9433-4ee0-9d30-c6c5ce7b0148.png) However, the download did still complete (using the default export format)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#5654