Nov. 14, 2024

Error boundaries in Django Templates

I wonder if Django could borrow the Error Boundary component concept from React? Now I know it won't map exactly as the Error Boundary in React is a client side thing and Django is server side. However I wonder if a similar concept could be applied when rendering templates.

If your not familiar with the Error Boundary, my understanding is that it is a component which limits how far an error can propagate within a React App. This means only a part of the page will error, leaving the rest of page to render successfully.

If this were in Django I would imagine the API to be exposed through the includes or block statements (or perhaps a new template tag?). Below is an example of how it could look.


{% block my_content %}
    
{% blockerror my_content%}
    
{% endblock my_content %}

or with includes it could be a special variable name? (I'm less sold on this version)

{% includes "myapp/snippets/row.html" with context_vars=here error_fallback="This errored" %}

Essentially the aim is to have partial errors when rendering a template in a standard manner. This needs testing in a package to see if it's even feasible, but I feel like it might be worth exploring rather than have the whole page exploding. This becomes even more relevant when used with HTMX or similar.

Or I am way off the mark? Let me know.