문제

Is there a handy function that converts absolute url to relative url in django?
Preferably, it would return the absolute url if base url doesn't actually match.

도움이 되었습니까?

해결책

You can use urlparse to get the relative path from the absolute uri. The following snippet is using urlparse from six so that it works both under PY2 and PY3.

def get_relative_url(absolute_uri):
    from six.moves.urllib.parse import urlparse
    return urlparse(absolute_uri).path
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top