Custom links allow users to display arbitrary hyperlinks to external content within NetBox object views. These are helpful for cross-referencing related records in systems outside NetBox. For example, you might create a custom link on the device view which links to the current device in a Network Monitoring System (NMS).
Custom links are created by navigating to Customization > Custom Links. Each link is associated with a particular NetBox object type (site, device, prefix, etc.) and will be displayed on relevant views. Each link has display text and a URL, and data from the NetBox item being viewed can be included in the link using Jinja template code through the variable object, and custom fields through object.cf.
For example, you might define a link like this:
View NMShttps://nms.example.com/nodes/?name={{ object.name }}When viewing a device named Router4, this link would render as:
<a href="https://nms.example.com/nodes/?name=Router4">View NMS</a>
Custom links appear as buttons in the top right corner of the page. Numeric weighting can be used to influence the ordering of links, and each link can be enabled or disabled individually.
!!! warning
Custom links rely on user-created code to generate arbitrary HTML output, which may be dangerous. Only grant permission to create or modify custom links to trusted users.
The following context data is available within the template when rendering a custom link's text or URL.
| Variable | Description |
|---|---|
object |
The NetBox object being displayed |
debug |
A boolean indicating whether debugging is enabled |
request |
A sanitized subset of the current request (see below) |
user |
The current user (if authenticated) |
perms |
The permissions assigned to the user |
!!! note "Changed in NetBox v4.7"
For security, `request` no longer exposes the full WSGI request object. Only a safe subset of attributes is available: `request.id`, `request.path`, `request.path_info`, `request.method`, `request.GET` (the query parameters), and `request.user` (the username). Sensitive data such as cookies, headers, and session state is no longer accessible from within a custom link template.
While most of the context variables listed above will have consistent attributes, the object will be an instance of the specific object being viewed when the link is rendered. Different models have different fields and properties, so you may need to some research to determine the attributes available for use within your template for a specific object type.
Checking the REST API representation of an object is generally a convenient way to determine what attributes are available. You can also reference the NetBox source code directly for a comprehensive list.
When the TEMPLATE_RENDERING configuration parameter selects restricted mode for custom links, links are rendered in a restricted environment which permits access only to plain data, and the context changes as follows:
| Variable | Description |
|---|---|
object |
The object's REST API representation. Related objects are fetched as they're accessed (e.g. object.device.tenant.group.slug), limited to those the user is permitted to view. |
user |
A dictionary holding the user's id and username |
perms |
A nested dictionary of the user's model permissions, so checks such as {% if perms.dcim.change_device %} keep working |
query() |
A function which returns objects matching the given REST API filters (e.g. query('ipam.ipaddress', device_id=object.id)), limited to those the user is permitted to view and to MAX_PAGE_SIZE objects |
debug and request are unchanged, except that request.GET holds the last value of each query parameter. In a table column, the objects on the current page are represented in a single batch.
Most links work the same in both modes. Common changes needed when migrating a link:
| Legacy | Restricted |
|---|---|
{{ object.cf.foo }} |
{{ object.custom_fields.foo }} |
{{ object.get_absolute_url() }} |
{{ object.display_url }} |
{% if object.status == 'active' %} |
{% if object.status.value == 'active' %} |
{{ object.get_status_display() }} |
{{ object.status.label }} |
{{ object.get_config_context() }} |
{{ object.config_context }} |
Only links which render with non-empty text are included on the page. You can employ conditional Jinja2 logic to control the conditions under which a link gets rendered.
For example, if you only want to display a link for active devices, you could set the link text to
{% if object.status == 'active' %}View NMS{% endif %}
The link will not appear when viewing a device with any status other than "active."
As another example, if you wanted to show only devices belonging to a certain manufacturer, you could do something like this:
{% if object.device_type.manufacturer.name == 'Cisco' %}View NMS{% endif %}
The link will only appear when viewing a device with a manufacturer name of "Cisco."
Group names can be specified to organize links into groups. Links with the same group name will render as a dropdown menu beneath a single button bearing the name of the group.
Custom links can also be included in object tables by selecting the desired links from the table configuration form. When displayed, each link will render as a hyperlink for its corresponding object. When exported (e.g. as CSV data), each link render only its URL.