Search code examples
pythondjangodjango-rest-frameworkapi-versioning

Django REST Framework URLPathVersioning not working


I followed the guide here to add versioning to our API. This is what the urls.py looks like:

from django.conf.urls import url
from django.contrib import admin
from django.urls import path

from api import views

urlpatterns = [    url(
        r'^(?P<version>(v1|v2))/foo/bar',
        views.foo_bar,
    ),
]

However, when I hit my API with the URL http://localhost:5555/v1/foo/bar I get an error:

TypeError at /v1/foo/bar
foo_bar() got an unexpected keyword argument 'version'

Solution

  • Most likely your foo_bar view does not accept the argument version.

    It needs to be defined as:

    def foo_bar(request, version):
        ...