20048 cleanup get_viewname URL resolution #1492

Closed
opened 2026-04-06 02:29:47 +02:00 by MrUnknownDE · 0 comments
Owner

Originally created by @arthanson on 8/7/2025

Fixes: #20048

adds a get_action_url to replace the get_viewname / reverse sequence in the app, and allows it to be replaceable by models that need a different URL pattern then the standard.

get_viewname is used in two filters viewname and validated_viewname which gets the viewname and then passes this to the url template tag so added in an action_url template tag (in netbox/utilities/templatetags/helpers.py) to use the get_action_url and have cleaner syntax goes from {% url object|viewname:'list' %} to {% action_url object 'list' %}. As a template tag it also supports as syntax so allows the following replacement:

{% with viewname=qs.model|validated_viewname:"list" %}
<a href="{% url viewname %}

with cleaner:

{% action_url qs.model 'list' as list_url %}
<a href="{{ list_url }}

netbox/extras/dashboard/widgets.py needed the returned context changed as viewname isn't returned anymore but the template just uses it in an error message, so just updated the error message.

NetBoxFeatureSet still uses get_viewname in the get_absolute_url function, but that is fine as any model that is overriding _get_action_url would also override get_absoulte_url as well so it doesn't need to be changed there.

NetBoxAPIHyperlinkedIdentityField and NetBoxURLHyperlinkedIdentityField also use get_viewname and there isn't a good way around that, but if needed any plugin should be able to override those as needed.

*Originally created by @arthanson on 8/7/2025* ### Fixes: #20048 adds a `get_action_url` to replace the `get_viewname` / reverse sequence in the app, and allows it to be replaceable by models that need a different URL pattern then the standard. `get_viewname` is used in two filters `viewname` and `validated_viewname` which gets the viewname and then passes this to the url template tag so added in an `action_url` template tag (in netbox/utilities/templatetags/helpers.py) to use the get_action_url and have cleaner syntax goes from `{% url object|viewname:'list' %}` to `{% action_url object 'list' %}`. As a template tag it also supports `as` syntax so allows the following replacement: ``` {% with viewname=qs.model|validated_viewname:"list" %} <a href="{% url viewname %} ``` with cleaner: ``` {% action_url qs.model 'list' as list_url %} <a href="{{ list_url }} ``` `netbox/extras/dashboard/widgets.py` needed the returned context changed as viewname isn't returned anymore but the template just uses it in an error message, so just updated the error message. NetBoxFeatureSet still uses get_viewname in the get_absolute_url function, but that is fine as any model that is overriding _get_action_url would also override get_absoulte_url as well so it doesn't need to be changed there. NetBoxAPIHyperlinkedIdentityField and NetBoxURLHyperlinkedIdentityField also use get_viewname and there isn't a good way around that, but if needed any plugin should be able to override those as needed.
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/netbox#1492