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.1.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

File details

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

File metadata

File hashes

Hashes for django_htmx_partial_redirect-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6790da1a9c8b121e80109079e303c9668eb04d532c33bb3021d1208b5cd30425
MD5 3adc2220aa1e774fd856f2abd1e644d9
BLAKE2b-256 254f951bf7e7b23f50ca635afe30f7d7835005dfb83e29f7b58fdbeb0c117732

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_htmx_partial_redirect-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b2a19af8611a4fe1fe4cd2874beb4070a10c9a73472b50069582b392199d49d5
MD5 a574f2e20c0ecfcec291d7238934de82
BLAKE2b-256 98411e3da1c3b19207bda7fea7fa1a6898ca5cc5c5fea612d493d87cfdd520c4

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