https://github.com/scriptogre/django-htmx-partial-redirect
This middleware redirects direct access to partial views (like login modals) to a full page, where content can be lazy-loaded via HTMX.
https://github.com/scriptogre/django-htmx-partial-redirect
django htmx modals partials
Last synced: 2 months ago
JSON representation
This middleware redirects direct access to partial views (like login modals) to a full page, where content can be lazy-loaded via HTMX.
- Host: GitHub
- URL: https://github.com/scriptogre/django-htmx-partial-redirect
- Owner: scriptogre
- Created: 2024-07-24T20:43:58.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-24T21:40:30.000Z (almost 2 years ago)
- Last Synced: 2025-08-28T05:41:51.441Z (10 months ago)
- Topics: django, htmx, modals, partials
- Language: Python
- Homepage: https://pypi.org/project/django-htmx-partial-redirect/
- Size: 3.91 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Django HTMX Partial Redirect Middleware
This Django middleware redirects direct accesses to HTMX partial views (e.g., modal content) to a full page, passing the original URL as a 'partial_url' parameter.
## Problem:
When a user directly accesses a URL (via address bar) that's meant to render a partial HTML view (with HTMX):
1. They see only the partial content (e.g., a login form).
2. The page lacks full context (CSS, JS, surrounding layout).
Example: Accessing `/login/` shows only a `` without styling or proper page structure.
## Solution:
This middleware:
1. Intercepts direct access to partial view URLs.
2. Redirects to a full page URL of your choice.
3. Passes the original partial URL as a parameter.
## Result:
1. User sees a complete, properly styled page.
2. The partial content (e.g., login form) can be lazy-loaded via HTMX by adding a check for the `partial_url` parameter in the template.
3. Partial content appears as intended (e.g., in a modal).
## How it works:
1. User accesses `/login/` directly.
2. Middleware redirects to `/?partial_url=/login/`.
3. Full page loads with HTMX trigger: `hx-trigger="load"`.
4. HTMX loads `/login/` content into the page (e.g., as a modal).
## Installation
1. Install the package:
```
pip install django-htmx-partial-redirect
```
2. Add the middleware to your `MIDDLEWARE` in `settings.py`:
```python
MIDDLEWARE = [
# ...
'django_htmx_partial_redirect.middleware.HTMXPartialViewRedirectMiddleware',
# ...
]
```
3. Configure the settings in your `settings.py`:
```python
HTMX_PARTIAL_VIEWS = ['login', 'signup', 'logout'] # List of view names to be treated as partial
HTMX_PARTIAL_VIEWS_REDIRECT_URL = '/' # URL to redirect to when accessing partial views directly
```
## Usage
In your base template, add this snippet at the end of the `` tag:
```html
{% if request.GET.partial_url %}
{% endif %}
```
Create a base modal template (e.g., `modal_base.html`):
```html
{% block content %}{% endblock %}
```
In your partial views (e.g., login, signup), extend the modal base:
```html
{% extends "modal_base.html" %}
{% block content %}
{% endblock %}
```
## How it works
The middleware intercepts requests to the specified HTMX partial views when they're accessed directly (not via HTMX). It then redirects to the specified full page URL, adding the original partial view URL as a `partial_url` parameter.
## Configuration
- `HTMX_PARTIAL_VIEWS`: A list of view names to be treated as HTMX partial views.
- `HTMX_PARTIAL_VIEWS_REDIRECT_URL`: The URL to redirect to when a partial view is accessed directly.
## Notes
If you don't want users to see the parameter in the URL (e.g. `/?partial_url=/login/`), you can use the `hx-push-url` attribute to update the URL in the address bar:
```html
```
## License
This project is licensed under the MIT License.