BAE
BAE

Reputation: 8936

django: same url but different verbs in Classed based view

Is it good to write urls.py as following:

urlpatterns = [
    url(r'^v1/files/$', FileView.as_view(), name='api-upload'),
    url(r'^v1/files/(?P<pk>\d+)$', FileView.as_view(), name='api-delete'),
    url(r'^v1/files/$', FileView.as_view(), name='api-view-all'),
    url(r'^v1/files/(?P<pk>\d+)$', FileView.as_view(), name='api-view-one'),
]

The second one and the forth one are nearly the same. But one is DELETE, the other is GET.

Any suggestions to improve it? Thanks. Is it possible to reverse the URL by django.core.urlresolvers? just like the following

'deleteUrl': reverse('upload-delete', args=[instance.pk]),

Upvotes: 0

Views: 574

Answers (1)

Gocht
Gocht

Reputation: 10256

You don't need to write urls, you just need to define two methods in your view:

# views.py

class FileView(...):

    def get(self, request, *args, **kwargs):
        # This method will catch the GET call

    def delete(self, request, *args, **kwargs):
        # This method will catch the DELETE call

With this, you will need only one url config:

url(r'^v1/files/(?P<pk>\d+)$', FileView.as_view(), name='api-file')

Upvotes: 1

Related Questions