Jinja2 condition not working anymore in Custom Link URL field #8106

Closed
opened 2025-12-29 20:32:31 +01:00 by adam · 3 comments
Owner

Originally created by @tux-mania on GitHub (May 24, 2023).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.5.2

Python version

3.9

Steps to Reproduce

  1. Create a custom link with jinja2 condition in "Link URL" field. For example, I set a new Custom Link named "website" with the following on the Link URL field:
{% if object.tenant_id == 1 %}
https://www.google.com
{% else %}
https://www.duckduckgo.com
{% endif %}

Expected Behavior

According to my object tenant id, it should open a webpage on google.com or duduckgo.com when clicking on the custom link button.

Observed Behavior

Instead, I got the following error:
netbox_sanitize_url

This behaviour seems related to the feature "Sanitize rendered custom link HTML" #12205 .
In fact, when removing the code added in the commit 89fa546, the expected behavior happens.

Thank you for your help.

Originally created by @tux-mania on GitHub (May 24, 2023). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.5.2 ### Python version 3.9 ### Steps to Reproduce 1. Create a custom link with jinja2 condition in "Link URL" field. For example, I set a new Custom Link named "website" with the following on the Link URL field: ``` {% if object.tenant_id == 1 %} https://www.google.com {% else %} https://www.duckduckgo.com {% endif %} ``` ### Expected Behavior According to my object tenant id, it should open a webpage on google.com or duduckgo.com when clicking on the custom link button. ### Observed Behavior Instead, I got the following error: ![netbox_sanitize_url](https://github.com/netbox-community/netbox/assets/57258906/cfac89b3-8b1a-4770-abec-c99a3cffc9ef) This behaviour seems related to the feature "Sanitize rendered custom link HTML" #12205 . In fact, when removing the code added in the commit [89fa546](https://github.com/netbox-community/netbox/commit/89fa546a1481e56d14cfc4d666a55482b0c85fd1), the expected behavior happens. Thank you for your help.
adam added the type: bugstatus: accepted labels 2025-12-29 20:32:31 +01:00
adam closed this issue 2025-12-29 20:32:31 +01:00
Author
Owner

@jeremystretch commented on GitHub (May 24, 2023):

The issue is that the rendered template produces errant line breaks before and after the URL (the %0A characters). We can tweak the code to tweak leading & trailing whitespace from the rendered template, but in the meantime I suggest altering your Jinja code to do this natively by adding hyphens to the block markers:

{% if object.tenant_id == 1 -%}
https://www.google.com
{%- else -%}
https://www.duckduckgo.com
{%- endif %}
@jeremystretch commented on GitHub (May 24, 2023): The issue is that the rendered template produces errant line breaks before and after the URL (the `%0A` characters). We can tweak the code to tweak leading & trailing whitespace from the rendered template, but in the meantime I suggest altering your Jinja code to [do this natively](https://jinja.palletsprojects.com/en/3.1.x/templates/#whitespace-control) by adding hyphens to the block markers: ``` {% if object.tenant_id == 1 -%} https://www.google.com {%- else -%} https://www.duckduckgo.com {%- endif %} ```
Author
Owner

@tux-mania commented on GitHub (May 24, 2023):

Hello @jeremystretch !
I didn't know about the hyphens. Works perfectly fine with this modification.
Thank you very much!

@tux-mania commented on GitHub (May 24, 2023): Hello @jeremystretch ! I didn't know about the hyphens. Works perfectly fine with this modification. Thank you very much!
Author
Owner

@jeremystretch commented on GitHub (May 24, 2023):

Glad that worked out, but I'd still like to strip whitespace from the rendered template to help avoid this issue.

@jeremystretch commented on GitHub (May 24, 2023): Glad that worked out, but I'd still like to strip whitespace from the rendered template to help avoid this issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8106