Add relative current_url
See original GitHub issueDescription
current_url
returns the full URL:
>>> request.htmx.current_url
'https://www.pythonmorsel.com/some-page/
I’d like a way to access just the non-domain part of this URL (I’m using it for a ?next=
login redirect).
>>> request.htmx.relative_current_url
'/some-page/
I imagine something like this might work.
@cached_property
def relative_current_url(self) -> str | None:
url = self.current_url
if url:
urlunparse(urlparse(url)._r(scheme='', netloc=''))
return url
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
html - URL redirection to be relative from current URL, how?
1 Answer 1 · Remove everything after the path · Remove everything after the last / · Append the relative path.
Read more >Relative paths considered relative to the current url (differs ...
Relative paths considered relative to the current url (differs from Sapper ... Add an option to svelte.config.js ( trailingSlashes: 'always' | 'never' ...
Read more >Apache: relative paths appended to current URL instead of ...
I was under the impression that relative paths are always resolved by appending them to the basename, not the current URL. This is...
Read more >URL() - Web APIs - MDN Web Docs - Mozilla
A string representing the base URL to use in cases where url is a relative URL. If not specified, it defaults to undefined...
Read more >Javascript get current url, domain from url, relative path ...
Because the current domain name that has been got does not include http://, when assigning it to the href of a tag, don't...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Added in #287, as
current_url_abs_path
.I did and a bit of research to check naming, and was reminded that relative URL’s mean “relative to a given base URL
, like
./image.pngis relative to whichever URL. The right name is “absolute-path” as per [section 4.2 of this RFC](https://www.rfc-editor.org/rfc/rfc3986#page-26), which is also what Django (kinda) uses e.g. in
Model.get_absolute_url(). So I picked the name with the
_abs_path` suffix, which is kinda wordy but at least correct.I do agree that the specific use case I showed is a hack that isn’t wise. That was the first case that came up and I haven’t yet refactored that code.
I just came across another case today though.
Something like this in a non-HTMX Django page:
Doesn’t have an equivalent in django-htmx land without a custom template filter/tag.
From my testing, passing an absolute URL to
next=
didn’t seem to work. Django seems to just ignore thenext
value entirely when it wasn’t a relative URL (as of Django 3.2 at least).Ah I do. Thanks for noting that!