Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get relative url from absolute url in django view?

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.

like image 910
eugene Avatar asked Oct 05 '22 00:10

eugene


1 Answers

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
like image 151
Dzhuang Avatar answered Oct 13 '22 12:10

Dzhuang