Search code examples
djangourldjango-rest-frameworkdjango-viewsurl-routing

Django url collisions


Using Django==2.2.11, djangorestframework==3.8.1

Thank you for reading!

The urls I am using that have the collision:

urlpatterns = [
    . . . 
    url(
        r'^some-path$',
        views.MyViewSet.as_view({'get': 'list'})
    ),
    url(
        r'^some-path$',
        views.MyViewSet.as_view({'post': 'create'}),
    ),
    ...
]

I am using postman to test each path, and it seems like there is a collision between these two urls.

Using this url with a GET, would work:

http://my_domain.com:8000/some-path

But POST with same url (and with a valid payload) would throw error:

WARNING 2020-03-28 19:13:57,288 "POST /some-path HTTP/1.1" 405 41

And response:

{"detail": "Method \"POST\" not allowed."}

I urls are swapped in order, then the POST would work, and GET would throw a similar error.

I looked at this post: 405 POST method not allowed

I would gladly add the view code - but I am pretty sure the issue is with the urls, since they each work when swapped order. Will add it upon request.

Thank you!

EDIT: I confused the urls- added the retrieve instead of list sorry!


Solution

  • If you are pointing to same end-point, ie /some-path, you should add your extra actions as,

    urlpatterns = [
        url(r'^some-path$', MusicianViewset.as_view({'post': 'create', 'get': 'list'})),
    ]