htmx with variable URL

Thanks to the ever-informative Django Chat, I’ve started taking a serious look at htmx, but I’ve encountered my first real stumbling block.

In Django I’m used to crafting urls with parameters as part of the url. (e.g. /app/function/<str:param>/)

Is there a way for me to craft the hx-get attribute to pass a variable to build that URL in a manner that fits my Django URL scheme? Or am I forced to convert these references to POST requests (or GET query variables) to make this work?

I think for this you’d possibly need an extension. It should be possible with the configRequest event - although the URL isn’t documented as sent in the even, it might just be. </> htmx - Events . If not you can probably request a change.

This request may also exist in the htmx issue tracker.

1 Like

The event object passed into the event listener function has an attribute, .detail.path containing the target URL. That value turns out to be editable and does result in the modified URL being called. Thanks!

Just as a follow-up in case you’re interested, I’ve (almost completely) migrated the Django Channels chat room tutorial to using htmx. The variable URL comes from the need to specify the name of the chat room being connected to. The htmx “handler” for the web socket gets rendered with the ws url generated from the name supplied.
It’s not quite done yet - it’s sending the messages fine, but I haven’t got the message reception working quite right yet. It is kinda cool. I’m starting to see the appeal of htmx. Thanks again for the pointer.

1 Like

Cool, is that tutorial available?

If you’re talking about the original Channels chat tutorial, it’s over on the Channels docs site.

If you’re talking about my PoC, I guess I could throw it out on github…

Yes, I was thinking about you PoC, just curious

No worries - when I get a chance, I’ll toss it up to github and post a link here.

Here you go.

2 Likes

Cool! Thanks
I’ll be looking at it.