Coverage for netbox/plugins/templates.py: 45%
29 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-10 18:35 +0000
1from django.template.loader import get_template
2from django.utils.translation import gettext as _
4__all__ = (
5 'PluginTemplateExtension',
6)
9class PluginTemplateExtension:
10 """
11 This class is used to register plugin content to be injected into core NetBox templates. It contains methods
12 that are overridden by plugin authors to return template content.
14 The `models` attribute on the class defines the which specific model detail pages this class renders content
15 for. It should be defined as a list of strings in the following form:
17 models = ['<app_label>.<model_name>', '<app_label>.<model_name>']
19 If `models` is left as None, the extension will render for _all_ models.
21 The `render()` method provides the following context data:
23 * object - The object being viewed (object views only)
24 * model - The type of object being viewed (list views only)
25 * request - The current request
26 * settings - Global NetBox settings
27 * config - Plugin-specific configuration parameters
28 """
29 models = None
31 def __init__(self, context):
32 self.context = context
34 def render(self, template_name, extra_context=None):
35 """
36 Convenience method for rendering the specified Django template using the default context data. An additional
37 context dictionary may be passed as `extra_context`.
38 """
39 if extra_context is None:
40 extra_context = {}
41 elif not isinstance(extra_context, dict):
42 raise TypeError(_("extra_context must be a dictionary"))
44 return get_template(template_name).render({**self.context, **extra_context})
46 #
47 # Global methods
48 #
50 def head(self):
51 """
52 HTML returned by this method will be inserted in the page's `<head>` block. This may be useful e.g. for
53 including additional Javascript or CSS resources.
54 """
55 raise NotImplementedError
57 def navbar(self):
58 """
59 Content that will be rendered inside the top navigation menu. Content should be returned as an HTML
60 string. Note that content does not need to be marked as safe because this is automatically handled.
61 """
62 raise NotImplementedError
64 #
65 # Object list views
66 #
68 def list_buttons(self):
69 """
70 Buttons that will be rendered and added to the existing list of buttons on the list view. Content
71 should be returned as an HTML string. Note that content does not need to be marked as safe because this is
72 automatically handled.
73 """
74 raise NotImplementedError
76 #
77 # Object detail views
78 #
80 def buttons(self):
81 """
82 Buttons that will be rendered and added to the existing list of buttons on the detail page view. Content
83 should be returned as an HTML string. Note that content does not need to be marked as safe because this is
84 automatically handled.
85 """
86 raise NotImplementedError
88 def alerts(self):
89 """
90 Arbitrary content to be inserted at the top of an object's detail view. Content should be returned as an
91 HTML string. Note that content does not need to be marked as safe because this is automatically handled.
92 """
93 raise NotImplementedError
95 def left_page(self):
96 """
97 Content that will be rendered on the left of the detail page view. Content should be returned as an
98 HTML string. Note that content does not need to be marked as safe because this is automatically handled.
99 """
100 raise NotImplementedError
102 def right_page(self):
103 """
104 Content that will be rendered on the right of the detail page view. Content should be returned as an
105 HTML string. Note that content does not need to be marked as safe because this is automatically handled.
106 """
107 raise NotImplementedError
109 def full_width_page(self):
110 """
111 Content that will be rendered within the full width of the detail page view. Content should be returned as an
112 HTML string. Note that content does not need to be marked as safe because this is automatically handled.
113 """
114 raise NotImplementedError