django-rest-framework/djangorestframework/reverse.py

21 lines
594 B
Python
Raw Normal View History

2012-02-22 00:47:55 +04:00
"""
Provide reverse functions that return fully qualified URLs
"""
from django.core.urlresolvers import reverse as django_reverse
from django.utils.functional import lazy
2012-02-22 00:47:55 +04:00
2012-02-23 13:21:01 +04:00
def reverse(viewname, *args, **kwargs):
2012-02-22 00:47:55 +04:00
"""
Same as `django.core.urlresolvers.reverse`, but optionally takes a request
and returns a fully qualified URL, using the request to get the base URL.
2012-02-22 00:47:55 +04:00
"""
request = kwargs.pop('request', None)
2012-02-22 00:47:55 +04:00
url = django_reverse(viewname, *args, **kwargs)
if request:
return request.build_absolute_uri(url)
return url
2012-02-22 00:47:55 +04:00
reverse_lazy = lazy(reverse, str)