{"id":25805672,"url":"https://github.com/scriptogre/django-htmx-partial-redirect","last_synced_at":"2026-04-12T02:36:27.017Z","repository":{"id":250055291,"uuid":"833335944","full_name":"scriptogre/django-htmx-partial-redirect","owner":"scriptogre","description":"This middleware redirects direct access to partial views (like login modals) to a full page, where content can be lazy-loaded via HTMX.","archived":false,"fork":false,"pushed_at":"2024-07-24T21:40:30.000Z","size":4,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-28T05:41:51.441Z","etag":null,"topics":["django","htmx","modals","partials"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/django-htmx-partial-redirect/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scriptogre.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-24T20:43:58.000Z","updated_at":"2025-06-06T16:25:26.000Z","dependencies_parsed_at":"2024-07-24T23:40:02.269Z","dependency_job_id":"7239b6c2-2079-458d-b5cd-765dcf4b4a1d","html_url":"https://github.com/scriptogre/django-htmx-partial-redirect","commit_stats":null,"previous_names":["scriptogre/django-htmx-partial-redirect"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/scriptogre/django-htmx-partial-redirect","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scriptogre%2Fdjango-htmx-partial-redirect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scriptogre%2Fdjango-htmx-partial-redirect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scriptogre%2Fdjango-htmx-partial-redirect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scriptogre%2Fdjango-htmx-partial-redirect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scriptogre","download_url":"https://codeload.github.com/scriptogre/django-htmx-partial-redirect/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scriptogre%2Fdjango-htmx-partial-redirect/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273528702,"owners_count":25121820,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","htmx","modals","partials"],"created_at":"2025-02-27T19:39:15.512Z","updated_at":"2026-04-12T02:36:21.882Z","avatar_url":"https://github.com/scriptogre.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Django HTMX Partial Redirect Middleware\n\nThis 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.\n\n## Problem:\n\nWhen a user directly accesses a URL (via address bar) that's meant to render a partial HTML view (with HTMX):\n1. They see only the partial content (e.g., a login form).\n2. The page lacks full context (CSS, JS, surrounding layout).\n\nExample: Accessing `/login/` shows only a `\u003cform\u003e` without styling or proper page structure.\n\n## Solution:\n\nThis middleware:\n1. Intercepts direct access to partial view URLs.\n2. Redirects to a full page URL of your choice.\n3. Passes the original partial URL as a parameter.\n\n## Result:\n\n1. User sees a complete, properly styled page.\n2. 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.\n3. Partial content appears as intended (e.g., in a modal).\n\n## How it works:\n\n1. User accesses `/login/` directly.\n2. Middleware redirects to `/?partial_url=/login/`.\n3. Full page loads with HTMX trigger: `hx-trigger=\"load\"`.\n4. HTMX loads `/login/` content into the page (e.g., as a modal).\n## Installation\n\n1. Install the package:\n   ```\n   pip install django-htmx-partial-redirect\n   ```\n\n2. Add the middleware to your `MIDDLEWARE` in `settings.py`:\n   ```python\n   MIDDLEWARE = [\n       # ...\n       'django_htmx_partial_redirect.middleware.HTMXPartialViewRedirectMiddleware',\n       # ...\n   ]\n   ```\n\n3. Configure the settings in your `settings.py`:\n   ```python\n   HTMX_PARTIAL_VIEWS = ['login', 'signup', 'logout']  # List of view names to be treated as partial\n   HTMX_PARTIAL_VIEWS_REDIRECT_URL = '/'  # URL to redirect to when accessing partial views directly\n   ```\n\n## Usage\n\nIn your base template, add this snippet at the end of the `\u003cbody\u003e` tag:\n\n```html\n{% if request.GET.partial_url %}\n   \u003cdiv hx-trigger=\"load\"\n        hx-get=\"{{ request.GET.partial_url }}\"\n        hx-swap=\"outerHTML\"\u003e\n   \u003c/div\u003e\n{% endif %}\n```\n\nCreate a base modal template (e.g., `modal_base.html`):\n\n```html\n\u003cdialog hx-on::load=\"this.showModal()\"\n        hx-on:close=\"this.remove()\"\u003e\n\n   \u003c!-- Backdrop (click to close) --\u003e\n   \u003cform method=\"dialog\" class=\"fixed inset-0 cursor-pointer -z-10\" hx-on:click=\"this.submit()\"\u003e\u003c/form\u003e\n\n   \u003c!-- Modal content --\u003e\n   \u003cdiv id=\"modal-content\"\u003e\n      {% block content %}{% endblock %}\n   \u003c/div\u003e\n\u003c/dialog\u003e\n```\n\nIn your partial views (e.g., login, signup), extend the modal base:\n\n```html\n{% extends \"modal_base.html\" %}\n\n{% block content %}\n\u003c!-- Your form or content here --\u003e\n{% endblock %}\n```\n\n## How it works\n\nThe 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.\n\n## Configuration\n\n- `HTMX_PARTIAL_VIEWS`: A list of view names to be treated as HTMX partial views.\n- `HTMX_PARTIAL_VIEWS_REDIRECT_URL`: The URL to redirect to when a partial view is accessed directly.\n\n## Notes\n\nIf 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:\n\n```html\n\u003cdiv hx-trigger=\"load\"\n     hx-get=\"{{ request.GET.partial_url }}\"\n     hx-swap=\"outerHTML\"\n     hx-push-url=\"/\"\u003e\n\u003c/div\u003e\n```\n\n## License\n\nThis project is licensed under the MIT License.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscriptogre%2Fdjango-htmx-partial-redirect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscriptogre%2Fdjango-htmx-partial-redirect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscriptogre%2Fdjango-htmx-partial-redirect/lists"}