Skip to main content
added 194 characters in body
Source Link
JohnG
  • 653
  • 8
  • 14

When I ran into this problemIf you're using django REST framework, I had access to ayou can use the reverse function from rest_framework.request.Requestreverse object. All I had to do was pass that request intoThis has the same behavior as django.core.urlresolvers.reverse(), except that it uses a request parameter to build a full URL.

from rest_framework.reverse import reverse # returns the full url url = reverse('view_name', args=(obj.pk,), request=request) # returns only the relative url url = reverse('view_name', args=(obj.pk,)) 

Edited to mention availability only in REST framework

When I ran into this problem, I had access to a rest_framework.request.Request object. All I had to do was pass that request into reverse().

# returns the full url url = reverse('view_name', args=(obj.pk,), request=request) # returns only the relative url url = reverse('view_name', args=(obj.pk,)) 

If you're using django REST framework, you can use the reverse function from rest_framework.reverse. This has the same behavior as django.core.urlresolvers.reverse, except that it uses a request parameter to build a full URL.

from rest_framework.reverse import reverse # returns the full url url = reverse('view_name', args=(obj.pk,), request=request) # returns only the relative url url = reverse('view_name', args=(obj.pk,)) 

Edited to mention availability only in REST framework

Source Link
JohnG
  • 653
  • 8
  • 14

When I ran into this problem, I had access to a rest_framework.request.Request object. All I had to do was pass that request into reverse().

# returns the full url url = reverse('view_name', args=(obj.pk,), request=request) # returns only the relative url url = reverse('view_name', args=(obj.pk,))