django / django/new-features

Http method routing

Open
#87 14 comments 16 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

Add http method routing to urlpatterns

### Problem

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

```python
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.

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

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

```python
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):

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

### Request or proposal

request

### Additional Details

forum [thread](https://forum.djangoproject.com/t/http-method-routing/43130)

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

https://code.djangoproject.com/ticket/33780

https://code.djangoproject.com/ticket/2784

https://groups.google.com/g/django-developers/c/FQVrKXkYAig

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?

### Implementation Suggestions

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.