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

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

Closed
tux-mania opened this issue May 24, 2023 · 3 comments
Closed

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

tux-mania opened this issue May 24, 2023 · 3 comments
Assignees
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@tux-mania
Copy link

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.

@tux-mania tux-mania added the type: bug A confirmed report of unexpected behavior in the application label May 24, 2023
@jeremystretch jeremystretch added the status: accepted This issue has been accepted for implementation label May 24, 2023
@jeremystretch jeremystretch self-assigned this May 24, 2023
@jeremystretch
Copy link
Member

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 %}

@tux-mania
Copy link
Author

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

@jeremystretch
Copy link
Member

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

@jeremystretch jeremystretch reopened this May 24, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

No branches or pull requests

2 participants