2017-09-11 45 views
0

基於方法處理同一網址上的視圖路由的最「pythonic」方法是什麼?我不喜歡解決方案切換相同的網址,但Django與休息框架不同的方法?

if(request.method == 'GET'): 
    ....... 

有沒有更好的方法?

+5

使用基於類的意見,建議在整個DRF文件。 –

+2

「ViewSet類僅僅是一種基於類的View,它不提供諸如.get()或.post()之類的任何方法處理程序,而是提供諸如.list()和.create()之類的操作。 「另見:http://www.django-rest-framework.org/api-guide/viewsets/#marking-extra-actions-for-routing – jpic

+1

借調視圖集。他們會和路由器一起照顧路由 – Linovia

回答

1

Django View是pythonic的代碼。

from django.http import HttpResponse 
from rest_framework.views import APIView 

class MyView(APIView): 
    def get(self, request): 
     # <view logic> 
     return HttpResponse('result') 
    def post(self, request): 
     # <view logic x2> 
     return HttpResponse('message_post_template') 

urls.py

from django.conf.urls import url 
from myapp.views import MyView 

urlpatterns = [ 
    url(r'^about/$', MyView.as_view(), name='view'), 
] 
相關問題