question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add relative current_url

See original GitHub issue

Description

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:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
adamchainzcommented, Nov 10, 2022

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. inModel.get_absolute_url(). So I picked the name with the _abs_path` suffix, which is kinda wordy but at least correct.

0reactions
treyhunnercommented, Sep 29, 2022

Yeah I think adding the helper is probably too niche/security sensitive to solve here. Calling Django’s redirect_to_login to generate a response that you then just use the URL from doesn’t sit right.

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:

{% url "users:login" %}?next={{ request.get_full_path }}

Doesn’t have an equivalent in django-htmx land without a custom template filter/tag.

{% url "users:login" %}?next={{ request.htmx.relative_current_url }}

(Django gets upset when it’s an absolute URL).

How exactly? redirect_to_login() doesn’t seem to mind. The later “allowed redirect” check should be done by the internal function url_has_allowed_host_and_scheme(), which checks for an allowed origin, so it should be fine with absolute URL’s on the same origin?

From my testing, passing an absolute URL to next= didn’t seem to work. Django seems to just ignore the next value entirely when it wasn’t a relative URL (as of Django 3.2 at least).

(P.S. you probably want to use urlsplit() instead of urlparse(), as I learned recently. Django could probably do with that as well…)

Ah I do. Thanks for noting that!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found