Add a generic view_tab.html template to ease plugin development #7822

Closed
opened 2025-12-29 20:28:40 +01:00 by adam · 5 comments
Owner

Originally created by @abhi1693 on GitHub (Mar 31, 2023).

Originally assigned to: @abhi1693 on GitHub.

NetBox version

v3.4.7

Feature type

Change to existing functionality

Proposed functionality

Add view_tab.html to templates/inc with the following minimalistic content

{% extends 'generic/object.html' %}
{% load helpers %}

{% block content %}
    {% include 'inc/table_controls_htmx.html' with table_modal=table_config %}
    <form method="post">
        {% csrf_token %}
        <div class="card">
            <div class="card-body" id="object_list">
                {% include 'htmx/table.html' %}
            </div>
        </div>
    </form>
{% endblock content %}

{% block modals %}
    {{ block.super }}
    {% table_config_form table %}
{% endblock modals %}

To allow table_config to be passed down to the template, make the following change in the python code

class ObjectChildrenView(ObjectView, ActionsMixin, TableMixin):
     table = None
     filterset = None
     template_name = 'inc/view_tabl.html'
 
+    def get_extra_context(self, request, instance):
+        return {
+            'table_config': f'{self.table.__name__}_config'
+        }
+
     def get_children(self, request, parent):
         """
         Return a QuerySet of child objects.

Use case

In a lot of my plugins, wherever I use the ViewTab functionality, I have to ship out my own template along with the code. In 99% cases, it's exactly the same file which I even reuse within the same plugin to avoid writing multiple ones for different classes. It would ease with the plugin development, if NetBox provided a generic template that the code will automatically look for if not specified by the plugin developer and use that.

Database changes

No response

External dependencies

No response

Originally created by @abhi1693 on GitHub (Mar 31, 2023). Originally assigned to: @abhi1693 on GitHub. ### NetBox version v3.4.7 ### Feature type Change to existing functionality ### Proposed functionality Add `view_tab.html` to `templates/inc` with the following minimalistic content ``` {% extends 'generic/object.html' %} {% load helpers %} {% block content %} {% include 'inc/table_controls_htmx.html' with table_modal=table_config %} <form method="post"> {% csrf_token %} <div class="card"> <div class="card-body" id="object_list"> {% include 'htmx/table.html' %} </div> </div> </form> {% endblock content %} {% block modals %} {{ block.super }} {% table_config_form table %} {% endblock modals %} ``` To allow `table_config` to be passed down to the template, make the following change in the python code ``` class ObjectChildrenView(ObjectView, ActionsMixin, TableMixin): table = None filterset = None template_name = 'inc/view_tabl.html' + def get_extra_context(self, request, instance): + return { + 'table_config': f'{self.table.__name__}_config' + } + def get_children(self, request, parent): """ Return a QuerySet of child objects. ``` ### Use case In a lot of my plugins, wherever I use the `ViewTab` functionality, I have to ship out my own template along with the code. In 99% cases, it's exactly the same file which I even reuse within the same plugin to avoid writing multiple ones for different classes. It would ease with the plugin development, if NetBox provided a generic template that the code will automatically look for if not specified by the plugin developer and use that. ### Database changes _No response_ ### External dependencies _No response_
adam added the status: acceptedtype: housekeeping labels 2025-12-29 20:28:40 +01:00
adam closed this issue 2025-12-29 20:28:41 +01:00
Author
Owner

@jeremystretch commented on GitHub (May 4, 2023):

IMO the only way it makes sense for us to add a template is if it can be utilized for core views as well. For ObjectChildrenView specifically, templates will typically inherit from a model-specific parent template anyway so I'm not sure how useful this would be.

@jeremystretch commented on GitHub (May 4, 2023): IMO the only way it makes sense for us to add a template is if it can be utilized for core views as well. For ObjectChildrenView specifically, templates will typically inherit from a model-specific parent template anyway so I'm not sure how useful this would be.
Author
Owner

@abhi1693 commented on GitHub (May 30, 2023):

I think this can be tackled on how I did for ObjectContactView template. I can provide a PR for this on a similar work and I hope we can simplify most of the templates in use.

@abhi1693 commented on GitHub (May 30, 2023): I think this can be tackled on how I did for `ObjectContactView` template. I can provide a PR for this on a similar work and I hope we can simplify most of the templates in use.
Author
Owner

@DanSheps commented on GitHub (Jun 6, 2023):

I don't see how useful a table would be for a "Tab View", IMO you would use ObjectChildrenView for that as it would likely be a related table or similar.

I think it would make sense if there was a generic view and it consisted of a single left pane which simply loops through all the models meta fields (perhaps have a base call to get_extra_content or similar which populates the attribute fields you want to display.

@DanSheps commented on GitHub (Jun 6, 2023): I don't see how useful a table would be for a "Tab View", IMO you would use ObjectChildrenView for that as it would likely be a related table or similar. I think it would make sense if there was a generic view and it consisted of a single left pane which simply loops through all the models meta fields (perhaps have a base call to get_extra_content or similar which populates the attribute fields you want to display.
Author
Owner

@abhi1693 commented on GitHub (Jun 6, 2023):

80% of my plugins use tab view to display related data in a table and all of them ship with the exact code to do this including the template file.

So, the idea was to reduce the code by introducing a boilerplate template which can be used by the core and plugins.

Since, there are cases where a template a template would need to provide extra functionality, we can provide blocks to do so.

@abhi1693 commented on GitHub (Jun 6, 2023): 80% of my plugins use tab view to display related data in a table and all of them ship with the exact code to do this including the template file. So, the idea was to reduce the code by introducing a boilerplate template which can be used by the core and plugins. Since, there are cases where a template a template would need to provide extra functionality, we can provide blocks to do so.
Author
Owner

@DanSheps commented on GitHub (Jun 6, 2023):

I think I missed the part where this was for ObjectChildrenView specifically, the mention of "View Tab" threw me off.

@DanSheps commented on GitHub (Jun 6, 2023): I think I missed the part where this was for ObjectChildrenView specifically, the mention of "View Tab" threw me off.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#7822