mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 09:16:10 -06:00
Catch exceptions when rendering device templates in UI
This commit is contained in:
parent
f92e39b2c9
commit
aa6440014a
@ -1,3 +1,5 @@
|
||||
import traceback
|
||||
|
||||
from django.contrib import messages
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.paginator import EmptyPage, PageNotAnInteger
|
||||
@ -10,6 +12,7 @@ from django.utils.html import escape
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import gettext as _
|
||||
from django.views.generic import View
|
||||
from jinja2.exceptions import TemplateError
|
||||
|
||||
from circuits.models import Circuit, CircuitTermination
|
||||
from extras.views import ObjectConfigContextView
|
||||
@ -2015,10 +2018,13 @@ class DeviceRenderConfigView(generic.ObjectView):
|
||||
context_data.update(**instance.get_config_context())
|
||||
|
||||
# Render the config template
|
||||
rendered_config = None
|
||||
if config_template := instance.get_config_template():
|
||||
rendered_config = config_template.render(context=context_data)
|
||||
else:
|
||||
rendered_config = None
|
||||
try:
|
||||
rendered_config = config_template.render(context=context_data)
|
||||
except TemplateError as e:
|
||||
messages.error(request, f"An error occurred while rendering the template: {e}")
|
||||
rendered_config = traceback.format_exc()
|
||||
|
||||
return {
|
||||
'config_template': config_template,
|
||||
|
@ -4,6 +4,7 @@ from django.db import models
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import gettext as _
|
||||
from jinja2.exceptions import TemplateError
|
||||
from jinja2.sandbox import SandboxedEnvironment
|
||||
|
||||
from extras.querysets import ConfigContextQuerySet
|
||||
|
@ -36,7 +36,7 @@
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<div class="card">
|
||||
{% if rendered_config %}
|
||||
{% if config_template %}
|
||||
<pre class="card-body">{{ rendered_config }}</pre>
|
||||
{% else %}
|
||||
<div class="card-body text-muted">No configuration template found</div>
|
||||
|
Loading…
Reference in New Issue
Block a user