Override netbox/templates/netbox/ipam/ipaddress_edit.html from a plugin #21258
-
|
Hi, I'm writing a simple plugin to auto pick IPs from certain ranges when creating a new IP. Specifically the /ipam/ip-addresses/add/ should have a small widget to do the picking. I found out the Reading from here https://django.readthedocs.io/en/stable/howto/overriding-templates.html I tried placing my own version in Is there an official way to do that? If not, what would be the cleanest way of approaching this? Thanks. Using netbox 4.4.0. Let me know if any more info is necessary. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
I believe you can’t replace existing forms or the templates used by their related views because they’re hard-coded. However, you can use a middleware to intercept specific requests and redirect them to a custom view instead. This custom view can then add additional logic. from collections.abc import Callable
from django.http import HttpRequest, HttpResponse
from django.shortcuts import redirect
from django.urls import resolve
class RedirectMiddleware:
def __init__(self, get_response: Callable) -> None:
self.get_response = get_response
def __call__(self, request: HttpRequest) -> HttpResponse:
resolver_match = resolve(request.path_info)
if resolver_match.view_name == "ipam:ipaddress_edit":
return redirect('plugins:myplugin:myformview')
return self.get_response(request)Warning Please note that this approach is extremely hacky and could break with each NetBox update. |
Beta Was this translation helpful? Give feedback.
I believe you can’t replace existing forms or the templates used by their related views because they’re hard-coded. However, you can use a middleware to intercept specific requests and redirect them to a custom view instead. This custom view can then add additional logic.