1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

58 lines
2.7 KiB
Markdown
Raw Normal View History

2019-08-28 12:39:11 -04:00
# Custom Links
2021-06-24 08:37:06 -04:00
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.
2019-08-28 12:39:11 -04:00
2021-06-24 08:37:06 -04:00
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 is assigned text and a URL, both of which support Jinja2 templating. The text and URL are rendered with the context variable `obj` representing the current object.
2019-08-28 12:39:11 -04:00
For example, you might define a link like this:
* Text: `View NMS`
* URL: `https://nms.example.com/nodes/?name={{ obj.name }}`
When viewing a device named Router4, this link would render as:
2020-08-03 10:53:44 -04:00
```no-highlight
2019-08-28 12:39:11 -04:00
<a href="https://nms.example.com/nodes/?name=Router4">View NMS</a>
```
2021-06-24 08:37:06 -04:00
Custom links appear as buttons in the top right corner of the page. Numeric weighting can be used to influence the ordering of links.
2019-08-28 12:39:11 -04:00
!!! 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.
## Context Data
The following context data is available within the template when rendering a custom link's text or URL.
| Variable | Description |
|----------|-------------|
| `obj` | The NetBox object being displayed |
| `debug` | A boolean indicating whether debugging is enabled |
| `request` | The current WSGI request |
| `user` | The current user (if authenticated) |
| `perms` | The [permissions](https://docs.djangoproject.com/en/stable/topics/auth/default/#permissions) assigned to the user |
2019-08-28 12:39:11 -04:00
## Conditional Rendering
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
2020-08-03 10:53:44 -04:00
```jinja2
{% if obj.status == 'active' %}View NMS{% endif %}
2019-08-28 12:39:11 -04:00
```
The link will not appear when viewing a device with any status other than "active."
2020-08-03 10:53:44 -04:00
As another example, if you wanted to show only devices belonging to a certain manufacturer, you could do something like this:
2019-09-13 12:08:48 -05:00
2020-08-03 10:53:44 -04:00
```jinja2
{% if obj.device_type.manufacturer.name == 'Cisco' %}View NMS{% endif %}
2019-09-13 12:08:48 -05:00
```
The link will only appear when viewing a device with a manufacturer name of "Cisco."
2019-08-28 12:39:11 -04:00
## Link Groups
2020-08-03 10:53:44 -04:00
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.