Skip to main content

`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

  1. Install the package:

    pip install django-htmx-partial-redirect
    
  2. Add the middleware to your MIDDLEWARE in settings.py:

    MIDDLEWARE = [
        # ...
        'django_htmx_partial_redirect.middleware.HTMXPartialViewRedirectMiddleware',
        # ...
    ]
    
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_htmx_partial_redirect-0.1.0.tar.gz (2.3 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django_htmx_partial_redirect-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django_htmx_partial_redirect-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a59e8051069a65f4897a51e8ecc479539c79da9d71965fd91e2e3e8d6a9927e0
MD5 74e9255f1e58445cb5e0ec1814f8937d
BLAKE2b-256 e82772591026134eff429bcc0f495f3b78517b4b8bd290a9479ce5840fafb0be

See more details on using hashes here.

File details

Details for the file django_htmx_partial_redirect-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_htmx_partial_redirect-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39d4193db4204e3187ca9b0882a412e86978213053808651f56c8e60b5a2b7f1
MD5 a2f65a53222bc78d4f99f3249e0eb32a
BLAKE2b-256 666b3cf7182e1606cf222c5478b45c3f98740ddb41f5e439748be93e1dc29adc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page