diff --git a/docs/api-guide/settings.md b/docs/api-guide/settings.md index 4f87b30da..6444bffd4 100644 --- a/docs/api-guide/settings.md +++ b/docs/api-guide/settings.md @@ -150,4 +150,8 @@ Default: `'accept'` Default: `'format'` +## USE_ABSOLUTE_URLS + +Default: `'True'` + [cite]: http://www.python.org/dev/peps/pep-0020/ diff --git a/docs/topics/release-notes.md b/docs/topics/release-notes.md index 35e8a8b35..361244c68 100644 --- a/docs/topics/release-notes.md +++ b/docs/topics/release-notes.md @@ -7,6 +7,7 @@ ## Master * Support for `read_only_fields` on `ModelSerializer` classes. +* Add support and settings option `USE_ABSOLUTE_URLS` ## 2.1.2 @@ -165,4 +166,4 @@ [cite]: http://www.catb.org/~esr/writings/cathedral-bazaar/cathedral-bazaar/ar01s04.html [2.1.0-notes]: https://groups.google.com/d/topic/django-rest-framework/Vv2M0CMY9bg/discussion -[announcement]: rest-framework-2-announcement.md +[announcement]: rest-framework-2-announcement.md \ No newline at end of file diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index d241ade7c..6f7c2dc06 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -1,4 +1,5 @@ from rest_framework import serializers +from rest_framework.settings import api_settings from rest_framework.templatetags.rest_framework import replace_query_param # TODO: Support URLconf kwarg-style paging @@ -15,7 +16,13 @@ class NextPageField(serializers.Field): return None page = value.next_page_number() request = self.context.get('request') - url = request and request.build_absolute_uri() or '' + url = '' + if request: + if api_settings.USE_ABSOLUTE_URLS: + url = request.build_absolute_uri() + else: + url = request.get_full_path() + return replace_query_param(url, self.page_field, page) @@ -30,7 +37,13 @@ class PreviousPageField(serializers.Field): return None page = value.previous_page_number() request = self.context.get('request') - url = request and request.build_absolute_uri() or '' + url = '' + if request: + if api_settings.USE_ABSOLUTE_URLS: + url = request.build_absolute_uri() + else: + url = request.get_full_path() + return replace_query_param(url, self.page_field, page) diff --git a/rest_framework/reverse.py b/rest_framework/reverse.py index c9db02f06..af1109bc1 100644 --- a/rest_framework/reverse.py +++ b/rest_framework/reverse.py @@ -1,6 +1,7 @@ """ Provide reverse functions that return fully qualified URLs """ +from rest_framework.settings import api_settings from django.core.urlresolvers import reverse as django_reverse from django.utils.functional import lazy @@ -14,7 +15,7 @@ def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra kwargs = kwargs or {} kwargs['format'] = format url = django_reverse(viewname, args=args, kwargs=kwargs, **extra) - if request: + if request and api_settings.USE_ABSOLUTE_URLS: return request.build_absolute_uri(url) return url diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 906a7cf6c..481d750c3 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -66,7 +66,9 @@ DEFAULTS = { 'URL_ACCEPT_OVERRIDE': 'accept', 'URL_FORMAT_OVERRIDE': 'format', - 'FORMAT_SUFFIX_KWARG': 'format' + 'FORMAT_SUFFIX_KWARG': 'format', + + 'USE_ABSOLUTE_URLS': True }