`django-htmx-partial-redirect` is a middleware that redirects direct accesses to partial views (like login modals) to a full page, where the partial content is then lazy-loaded via HTMX, solving the issue of empty pages when accessing modal-intended views directly.
Project description
Django Partial View Redirect Middleware
This Django middleware redirects direct accesses to partial views (e.g., HTMX modal content) to a full page, passing the original URL as a partial_url
parameter.
Installation
-
Install the package:
pip install django-htmx-partial-redirect
-
Add the middleware to your
MIDDLEWARE
insettings.py
:MIDDLEWARE = [ # ... 'django_htmx_partial_redirect.middleware.HTMXPartialViewRedirectMiddleware', # ... ]
-
Configure the settings in your
settings.py
:HTMX_PARTIAL_VIEWS = ['login', 'signup', 'todos:add'] # List of view names to be treated as partial HTMX_PARTIAL_VIEWS_REDIRECT_URL = '/home/' # URL to redirect to when accessing partial views directly
Usage
In your view or template, you can check for the partial_url
parameter and load the partial content using lazy loading (via hx-trigger="load"
):
{% if request.GET.partial_url %}
<div hx-get="{{ request.GET.partial_url }}"
hx-target="body"
hx-swap="beforeend"
hx-trigger="load">
</div>
{% endif %}
How it works
The middleware intercepts requests to the specified 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 partial views.HTMX_PARTIAL_VIEWS_REDIRECT_URL
: The URL to redirect to when a partial view is accessed directly.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_htmx_partial_redirect-0.1.0.tar.gz
.
File metadata
- Download URL: django_htmx_partial_redirect-0.1.0.tar.gz
- Upload date:
- Size: 2.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a59e8051069a65f4897a51e8ecc479539c79da9d71965fd91e2e3e8d6a9927e0 |
|
MD5 | 74e9255f1e58445cb5e0ec1814f8937d |
|
BLAKE2b-256 | e82772591026134eff429bcc0f495f3b78517b4b8bd290a9479ce5840fafb0be |
File details
Details for the file django_htmx_partial_redirect-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: django_htmx_partial_redirect-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.3 CPython/3.12.4 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39d4193db4204e3187ca9b0882a412e86978213053808651f56c8e60b5a2b7f1 |
|
MD5 | a2f65a53222bc78d4f99f3249e0eb32a |
|
BLAKE2b-256 | 666b3cf7182e1606cf222c5478b45c3f98740ddb41f5e439748be93e1dc29adc |