-
DescriptionI'm trying out litestar with htmx and I encountered this problem while following the documentation `AttributeError: 'Request' object has no attribute 'htmx' URL to code causing the issueNo response MCVEfrom pathlib import Path
from litestar import get, Litestar
from litestar.response import Template
from litestar.template import TemplateConfig
from litestar.contrib.jinja import JinjaTemplateEngine
from litestar.contrib.htmx.request import HTMXRequest
from litestar.contrib.htmx.response import HTMXTemplate
@get("/")
async def index() -> Template:
return Template(template_name="pages/index.html")
@get(path="/form")
async def get_form(request: HTMXRequest) -> Template:
htmx = request.htmx # if true will return HTMXDetails class object
if htmx:
print(htmx.current_url)
context = {}
return HTMXTemplate(
template_name="partials/sample.html",
context=context,
push_url="/form",
)
template_config = TemplateConfig(
directory=Path(__file__).parent / "templates",
engine=JinjaTemplateEngine,
)
app = Litestar(
route_handlers=[index, get_form],
template_config=template_config,
) <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Simply HTMX</title>
<link href="" rel="stylesheet">
<script src="https://unpkg.com/htmx.org"></script>
</head>
<body>
<button id="test-button" class="button is-primary" hx-get="/form" hx-swap="outerHTML">
I'm new here...
</button>
</body>
</html> Steps to reproduce1. Run the script or the sample repository here https://github.com/yujinyuz/simplystar
2. Click the button
3. You should get the error Screenshots"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)" LogsNo response Litestar Version2.11.0 Platform
Note While we are open for sponsoring on GitHub Sponsors and Check out all issues funded or available for funding on our Polar.sh dashboard
|
Beta Was this translation helpful? Give feedback.
Answered by
Alc-Alc
Sep 8, 2024
Replies: 1 comment 3 replies
-
You will have to specify |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
yujinyuz
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will have to specify
request_class=HTMXRequest
at the app or route handler layer. This is not a bug.