If you have access to an HttpRequest
instance, you can use HttpRequest.build_absolute_uri(location)
:
absolute_uri = request.build_absolute_uri(relative_uri)
In alternative, you can get it using the sites framework:
import urlparse
from django.contrib.sites.models import Site
domain = Site.objects.get_current().domain
absolute_uri = urlparse.urljoin('http://{}'.format(domain), relative_uri)
Re: Edit1
I tend to use urlparse.join
, because it's in the standard library and it's technically the most Pythonic way to combine URIs, but I think that your approach is fine too.