Http method routing

This doesn’t work right now because the first url pattern will always resolve.

urlpatterns = [
    path('pet', views.pet_list_view),
    path('pet', views.pet_create_view),
]

I understand that we can solve this built in right now by routing on class methods.

urlpatterns = [
    path('pet', views.PetView.as_view())
]

Also, it can be solved functionally by making our own “routing” view:

urlpatterns = [
    path('pet', list_router(get=views.pet_list_view, post=views.pet_create_view),
]

It also seems like it would be nice if this “just worked” (or some other chosen syntax):

urlpatterns = [
    path('pet', views.pet_list_view, methods=['GET'],),
    path('pet', views.pet_create_view, methods=['POST']),
]

I found some prior tickets/conversation, but nothing really seems to suggest there’s a blocker:

I think this would be a nice addition to django especially for building some simple json apis. Looking at the UrlResolver code, it seems like not the type of code to try and implement as a “django app”. Thoughts?

2 Likes

For completeness: another way that you would do it right now, without CBV or routers, would be to create a list_or_create view and check request.method and then branch off.

I would personally like to have a well-supported way of doing this that isn’t making an intermediate view or using a CBV. The syntax also seems okay to me. I would suggest adding this to the new-features repo as that’s where most discussion of new features happens now.

2 Likes

new feature ticket opened. I also don’t expect someone else to do this; I am happy to implement it. Making a change to url routing seemed like such a fundamental thing that I wanted some +1s and some core dev support before touching a pr.