I see: so you want to use export templates to generate pretty HTML pages.
I had a quick look into this. The problem is that Netbox uses
jinja2.Environment().from_string(...) to parse templates stored in the model. It says
here:
Using a template loader rather than passing strings to Template
or Environment.from_string()
has multiple advantages. Besides being a lot easier to use it also enables template inheritance. There is a 'loader' attribute on
Environment; maybe setting it to an instance of
FileSystemLoader would work. You could try something like this (completely untested):
--- netbox/extras/models.py.orig 2019-09-04 20:37:21.221751307 +0000
+++ netbox/extras/models.py 2019-09-17 16:19:49.180264520 +0000
@@ -12,7 +12,7 @@
from django.template import Template, Context
from django.urls import reverse
import graphviz
-from jinja2 import Environment
+from jinja2 import Environment, FileSystemLoader
from taggit.models import TagBase, GenericTaggedItemBase
from dcim.constants import CONNECTION_STATUS_CONNECTED
@@ -467,7 +467,9 @@
output = template.render(Context(context))
elif self.template_language == TEMPLATE_LANGUAGE_JINJA2:
- template = Environment().from_string(source=self.template_code)
+ template = Environment(
+ loader=FileSystemLoader("/opt/netbox/netbox/templates/export")
+ ).from_string(source=self.template_code)
output = template.render(**context)
else:
If it works, you could raise it as a FR. (To do it properly, it would need to be modified to use TEMPLATES_DIR from netbox/settings.py). But it's not a great solution IMO, as it means parts of your export templates would be in the database, and parts in the filesystem.
So what would probably be better is to write a custom
Loader (subclass of BaseLoader), which is able to load other templates by name from the export templates table in the database.