You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/plugins/development/views.md
+44-9Lines changed: 44 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Views
2
2
3
-
## Writing Views
3
+
## Writing Basic Views
4
4
5
5
If your plugin will provide its own page or pages within the NetBox web UI, you'll need to define views. A view is a piece of business logic which performs an action and/or renders a page when a request is made to a particular URL. HTML content is rendered using a [template](./templates.md). Views are typically defined in `views.py`, and URL patterns in `urls.py`.
6
6
@@ -47,9 +47,13 @@ A URL pattern has three components:
47
47
48
48
This makes our view accessible at the URL `/plugins/animal-sounds/random/`. (Remember, our `AnimalSoundsConfig` class sets our plugin's base URL to `animal-sounds`.) Viewing this URL should show the base NetBox template with our custom content inside it.
49
49
50
+
## NetBox Model Views
51
+
52
+
NetBox provides several generic view classes and additional helper functions, to simplify the implementation of plugin logic. These are recommended to be used whenever possible to keep the maintenance overhead of plugins low.
53
+
50
54
### View Classes
51
55
52
-
NetBox provides several generic view classes (documented below) to facilitate common operations, such as creating, viewing, modifying, and deleting objects. Plugins can subclass these views for their own use.
56
+
Generic view classes (documented below) facilitate common operations, such as creating, viewing, modifying, and deleting objects. Plugins can subclass these views for their own use.
@@ -65,18 +69,51 @@ NetBox provides several generic view classes (documented below) to facilitate co
65
69
!!! warning
66
70
Please note that only the classes which appear in this documentation are currently supported. Although other classes may be present within the `views.generic` module, they are not yet supported for use by plugins.
67
71
68
-
#### Example Usage
72
+
### URL registration
73
+
74
+
The NetBox URL registration process has two parts:
75
+
76
+
1. View classes can be decorated with `@register_model_view()`. This registers a new URL for the model.
77
+
2. All of a model's URLs can be included in `urls.py` using the `get_model_urls()` function. This call is usually required twice: once to import general views for the model and again to import model detail views tied to the object's primary key.
78
+
79
+
::: utilities.views.register_model_view
80
+
81
+
!!! note "Changed in NetBox v4.2"
82
+
In NetBox v4.2, the `register_model_view()` function was extended to support the registration of list views by passing `detail=False`.
83
+
84
+
::: utilities.urls.get_model_urls
85
+
86
+
!!! note "Changed in NetBox v4.2"
87
+
In NetBox v4.2, the `get_model_urls()` function was extended to support retrieving registered general model views (e.g. for listing objects) by passing `detail=False`.
Below are the class definitions for NetBox's object views. These views handle CRUD actions for individual objects. The view, add/edit, and delete views each inherit from `BaseObjectView`, which is not intended to be used directly.
@@ -143,6 +180,9 @@ Below are the class definitions for NetBox's multi-object views. These views han
143
180
144
181
These views are provided to enable or enhance certain NetBox model features, such as change logging or journaling. These typically do not need to be subclassed: They can be used directly e.g. in a URL path.
145
182
183
+
!!! note
184
+
These feature views are automatically registered for all models that implement the respective feature. There is usually no need to override them. However, if that's the case, the URL must be registered manually in `urls.py` instead of using the `register_model_view()` function or decorator.
185
+
146
186
::: netbox.views.generic.ObjectChangeLogView
147
187
options:
148
188
members:
@@ -157,7 +197,7 @@ These views are provided to enable or enhance certain NetBox model features, suc
157
197
158
198
### Additional Tabs
159
199
160
-
Plugins can "attach" a custom view to a core NetBox model by registering it with `register_model_view()`. To include a tab for this view within the NetBox UI, declare a TabView instance named `tab`, and add it to the template context dict:
200
+
Plugins can "attach" a custom view to a NetBox model by registering it with `register_model_view()`. To include a tab for this view within the NetBox UI, declare a TabView instance named `tab`, and add it to the template context dict:
161
201
162
202
```python
163
203
from dcim.models import Site
@@ -185,11 +225,6 @@ class MyView(generic.ObjectView):
185
225
)
186
226
```
187
227
188
-
!!! note "Changed in NetBox v4.2"
189
-
The `register_model_view()` function was extended in NetBox v4.2 to support registration of list views by passing `detail=False`.
0 commit comments