from django.http import HttpRequest, HttpResponse
from django.utils.safestring import mark_safe
from django_components import Component, types
class ErrorFallbackPage(Component):
class Media:
js = (
mark_safe(
''
),
)
template: types.django_html = """
{% load component_tags %}
Weather API Widget Example
This example demonstrates using ErrorFallback to handle potential API failures gracefully.
Case 1: API call is successful
{% component "error_fallback" %}
{% fill "content" %}
{% component "weather_widget" location="New York" / %}
{% endfill %}
{% fill "fallback" %}
Error:
Could not load weather data.
{% endfill %}
{% endcomponent %}
Case 2: API call fails
{% component "error_fallback" %}
{% fill "content" %}
{% component "weather_widget" location="Atlantis" simulate_error=True / %}
{% endfill %}
{% fill "fallback" %}
Error:
Could not load weather data for
Atlantis.
The location may not be supported or the service is temporarily down.
{% endfill %}
{% endcomponent %}
""" # noqa: E501
class View:
def get(self, request: HttpRequest) -> HttpResponse:
return ErrorFallbackPage.render_to_response(request=request)