2016-07-07 58 views
0

這是好寫urls.py如下:Django的:基於視圖分班相同的URL,但不同的動詞

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'), 
] 

第二個和第四一個幾乎相同。但一個是DELETE,另一個是GET。

有什麼建議可以改進它?謝謝。是否有可能通過django.core.urlresolvers反向URL?就像下面

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

回答

0

你並不需要寫的網址,你只需要定義視圖兩種方法:

# 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 

有了這個,你將只需要一個網址配置:

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

是否有可能通過django.core.urlresolvers反向URL? – BAE

+0

@BAE拜託,你能舉一個你想要的例子嗎? – Gocht

相關問題