-
-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
requires_HTMX decorator #205
Comments
I don't think I would personally use this. Being able to load up a view that returns a fragment for htmx can be useful for debugging at least. And I can't think of a common way that users would accidentally hit such URL's, since they're only mentioned in hx-* attrs normally? I've never seen a JSON API that analogously detects you're visiting from a browser and redirects you. Can you explain more of what lead you to think of this? |
I've had some experiences with my browser showing the raw output from a HTMX request when for instance accessing a page again after the browser being closed. It might have something to do with wrong usage of hx-push-url. My reasoning was that if I could just block of that kind of direct access, I would minimise those kinds of weird "accidental behaviour". The idea with the decorator is to mimic the django builtin I also thought of including a call to |
Indeed you shouldn't push URL's into the location history that the user cannot load directly. Such URL's may be loaded in many situations: when the browser restarts, or the internet goes offline and returns, or if the user hits refresh. Displaying "fake" not-directly-loadable URL's seems like an anti-pattern to me. Because of this I do not see the utility of the decorator. |
Oh but it was not non-directly-loadable URLs I was pushing. I had just guessing that hx-push-url might be the reason - I'm not sure if it was. Those random encounters (which I'll have to see if I can replicate somehow) were reason for the decorator. But I think that it has more merit than just avoiding that case - in the same way as |
Please come back with more info then :) I think there's a key difference with |
Will do! The decorator could configured to be disabled if DEBUG=True - also as an opt-in it is/would be just a safe-guard against wrong usage if one chooses to use it. Having it redirect could also be optional, making it more akin to |
Description
I've implemented the following decorator and found it quite useful and just wanted to share.
The idea is to replicate the
requires_http_methods
variants likerequires_POST
, and redirect the user to a more relevant page if they (or their browser) somehow accidentally try to access a HTMX only endpoint. I've at least had some oddities of somehow getting there.The decorator is pretty straight forward:
Basic usage:
but can also be used to redirect the user to a page which is relevant to the view:
which is quite useful when dealing with many HTMX only views tied together.
If this makes sense and you could see it work in django-htmx, I would love to do a PR with tests and documentation @adamchainz - if it is out of scope for the project that is also fine :)
The text was updated successfully, but these errors were encountered: