Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lllama/starlette-htmx
https://github.com/lllama/starlette-htmx
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lllama/starlette-htmx
- Owner: lllama
- License: mit
- Created: 2022-01-21T13:09:52.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-21T22:37:08.000Z (almost 3 years ago)
- Last Synced: 2024-07-17T08:26:48.385Z (4 months ago)
- Language: Python
- Size: 2.93 KB
- Stars: 16
- Watchers: 1
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-python-htmx - Starlette_htmx - Starlette-a9bbcc?style=flat&logo=starlette&logoColor=black" alt="Starlette"></a><br/> (Third Party Packages 📦 <a name = "tools"></a> / Helper Libraries)
README
# Starlette_htmx
A set of extensions for using [HTMX](https://htmx.org) with [Starlette](http://starlette.io).
Based on [django-htmx](https://github.com/adamchainz/django-htmx) by [Adam Johnson](https://github.com/adamchainz)
## Middleware
Usage:
```python
from starlette.middleware import Middleware
from starlette_htmx.middleware import HtmxMiddlewareapp = Starlette(debug=True, ..., middleware=[Middleware(HtmxMiddleware)])
```The request objects will then have a `request.state.htmx` object which you can
use to test whether the request was made by htmx.This class provides shortcuts for reading the htmx-specific `request headers `__.
### `__bool__(): bool`
`True` if the request was made with htmx, otherwise `False`.
This is based on the presence of the `HX-Request` header.This allows you to switch behaviour for requests made with htmx like so:
```python
def my_view(request):
if request.htmx:
template_name = "partial.html"
else:
template_name = "complete.html"
return render(template_name, ...)
```### `boosted: bool`
`True` if the request came from an element with the `hx-boost` attribute.
Based on the `HX-Boosted` header.### `current_url: str | None`
The current URL of the browser, or `None` for non-htmx requests.
Based on the `HX-Current-URL` header.### `history_restore_request: bool`
`True` if the request is for history restoration after a miss in the local history cache.
Based on the `HX-History-Restore-Request` header.### `prompt: str | None`
The user response to `hx-prompt `__ if it was used, or `None`.
### `target: str | None`
The `id` of the target element if it exists, or `None`.
Based on the `HX-Target` header.### `trigger: str | None`
The `id` of the triggered element if it exists, or `None`.
Based on the `HX-Trigger` header.### `trigger_name: str | None`
The `name` of the triggered element if it exists, or `None`.
Based on the `HX-Trigger-Name` header.### `triggering_event: Any | None`
The deserialized JSON representtation of the event that triggered the request if it exists, or `None`.
This header is set by the `event-header htmx extension
`__, and contains details of the DOM
event that triggered the request.