Reputation: 1128
I am making a Django-based API and here is my urls.py.
from django.conf.urls import include, url
api_version = "v1"
urlpatterns = [
url(r'^v1/', include( "api."+ api_version +".urls", namespace=api_version )),
]
What I want to do is to retrieve API version from http accept header. I tried to use django.http.HttpRequest module, which didn't do the trick.
Is there any way to achieve this?
Upvotes: 2
Views: 1361
Reputation: 3965
You cannot access request in url.py, for your particular scnerio i-e for version, you can use different url for different version of your api.
like
url(r'^v1/', include("api.v1.urls")),
url(r'^v2/', include("api.v2.urls")),
url(r'^v3/', include("api.v3.urls")),
Upvotes: 0
Reputation: 96
You can't access request on urls.py
, see: How Django processes a request
You can configure versioning on django-rest-framework and get version
on request object like:
class SampleView(APIView):
def post(self, request, format=None):
if request.version == '1':
return Response(status=status.HTTP_410_GONE)
Or use URLPathVersioning
approach.
Upvotes: 1