mirror of
https://github.com/django-components/django-components.git
synced 2025-11-20 06:45:32 +00:00
docs: Add "scenarios" code examples (#1445)
Some checks are pending
Docs - build & deploy / docs (push) Waiting to run
Run tests / build (ubuntu-latest, 3.10) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.11) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.12) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.13) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.8) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.9) (push) Waiting to run
Run tests / build (windows-latest, 3.10) (push) Waiting to run
Run tests / build (windows-latest, 3.11) (push) Waiting to run
Run tests / build (windows-latest, 3.12) (push) Waiting to run
Run tests / build (windows-latest, 3.13) (push) Waiting to run
Run tests / build (windows-latest, 3.8) (push) Waiting to run
Run tests / build (windows-latest, 3.9) (push) Waiting to run
Run tests / test_docs (3.13) (push) Waiting to run
Run tests / test_sampleproject (3.13) (push) Waiting to run
Some checks are pending
Docs - build & deploy / docs (push) Waiting to run
Run tests / build (ubuntu-latest, 3.10) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.11) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.12) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.13) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.8) (push) Waiting to run
Run tests / build (ubuntu-latest, 3.9) (push) Waiting to run
Run tests / build (windows-latest, 3.10) (push) Waiting to run
Run tests / build (windows-latest, 3.11) (push) Waiting to run
Run tests / build (windows-latest, 3.12) (push) Waiting to run
Run tests / build (windows-latest, 3.13) (push) Waiting to run
Run tests / build (windows-latest, 3.8) (push) Waiting to run
Run tests / build (windows-latest, 3.9) (push) Waiting to run
Run tests / test_docs (3.13) (push) Waiting to run
Run tests / test_sampleproject (3.13) (push) Waiting to run
This commit is contained in:
parent
71c489df8e
commit
49afdb49d6
49 changed files with 1550 additions and 260 deletions
55
docs/examples/error_fallback/README.md
Normal file
55
docs/examples/error_fallback/README.md
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
# Error handling
|
||||
|
||||
The built-in [`ErrorFallback`](../../reference/components/#django_components.components.error_fallback.ErrorFallback) component catches errors during component rendering and displays fallback content instead. This is similar to React's [`ErrorBoundary`](https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary) component.
|
||||
|
||||
In this scenario, we have a `WeatherWidget` component that simulates fetching data from a weather API,
|
||||
which we wrap in the built-in [`ErrorFallback`](../../reference/components/#django_components.components.error_fallback.ErrorFallback) component.
|
||||
|
||||
We have two cases:
|
||||
|
||||
1. API call succeeds. The `WeatherWidget` component renders the weather information as expected.
|
||||
2. API call fails. The `ErrorFallback` component catches the error and display a user-friendly message instead of breaking the page.
|
||||
|
||||
```django
|
||||
{% component "error_fallback" %}
|
||||
{% fill "content" %}
|
||||
{% component "weather_widget" location="Atlantis" / %}
|
||||
{% endfill %}
|
||||
{% fill "fallback" %}
|
||||
<p style="color: red;">
|
||||
Could not load weather data for <strong>Atlantis</strong>.
|
||||
The location may not be supported or the service is temporarily down.
|
||||
</p>
|
||||
{% endfill %}
|
||||
{% endcomponent %}
|
||||
```
|
||||
|
||||

|
||||
|
||||
## Definition
|
||||
|
||||
```djc_py
|
||||
--8<-- "docs/examples/error_fallback/component.py"
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
To see the component in action, you can set up a view and URL pattern as shown below.
|
||||
|
||||
### `views.py`
|
||||
|
||||
```djc_py
|
||||
--8<-- "docs/examples/error_fallback/page.py"
|
||||
```
|
||||
|
||||
### `urls.py`
|
||||
|
||||
```python
|
||||
from django.urls import path
|
||||
|
||||
from examples.pages.error_fallback import ErrorFallbackPage
|
||||
|
||||
urlpatterns = [
|
||||
path("examples/error_fallback", ErrorFallbackPage.as_view(), name="error_fallback"),
|
||||
]
|
||||
```
|
||||
Loading…
Add table
Add a link
Reference in a new issue