2017-05-26 52 views
0

我對Django相對較新& Django休息 - 以前只建立非常簡單的應用程序。 目前正面臨使用嵌套路線的問題。 這裏是我的相關CONFIGS:Django休息嵌套APIView路線

主urls.py:

urlpatterns = [ 
    url(r'^'+root_url+'/swagger', swagger_schema_view), 
    url(r'^' + root_url + '/', include('payments.urls')), 
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

應用程序的urls.py:

urlpatterns = [ 
    url(r'payments', views.PaymentsView.as_view(), name='index'), 
    url(r'payments/charge', views.PaymentsChargeView.as_view(), name='charge'), 
] 

應用的看法:

import logging 

from django.views.decorators.csrf import csrf_exempt 
from django.utils.decorators import method_decorator 
from rest_framework.authentication import BasicAuthentication 
from mysite.csrf_exempt import CsrfExemptSessionAuthentication 

from rest_framework.views import APIView 
from rest_framework.response import Response 

import stripe 
try: 
    from django.conf import settings 
except ImportError: 
    pass 



logger = logging.getLogger(__name__) 


@method_decorator(csrf_exempt, name='dispatch') 
class PaymentsView(APIView): 
    authentication_classes = (CsrfExemptSessionAuthentication, BasicAuthentication) 

    def get(self, request, *args, **kwargs): 
     print('here GET PaymentsView') 
     return Response('good') 

    def post(self, request, *args, **kwargs): 
     print('here POST PaymentsView') 
     return Response('good') 


@method_decorator(csrf_exempt, name='dispatch') 
class PaymentsChargeView(APIView): 
    authentication_classes = (CsrfExemptSessionAuthentication, BasicAuthentication) 

    def get(self, request, *args, **kwargs): 
     print('here GET PaymentsChargeView') 
     return Response('good') 

    def post(self, request, *args, **kwargs): 
     print('here POST PaymentsChargeView') 
     return Response('good post') 

問題:

請求既/payments/payments/charge GET/POST總是PaymentsView處理(例如:POST/payments/payments/charge讓我在控制檯 '在這裏發表PaymentsView')

回答

1

最好的做法是把$ (end-of-string match character)在你的網址。所以定義的url將匹配並處理正確的視圖函數。

url(r'payments$', views.PaymentsView.as_view(), name='index'), 
url(r'payments/charge$', views.PaymentsChargeView.as_view(), name='charge'), 
+0

謝謝,解決了一個概率 – user1935987

+0

很高興它的工作! –

0

更改您的網址訂單

urlpatterns = [ 
    url(r'payments/charge', views.PaymentsChargeView.as_view(), name='charge'), 
    url(r'payments', views.PaymentsView.as_view(), name='index'), 
] 
0
urlpatterns = [ 
    url(r'^payments', views.PaymentsView.as_view(), name='index'), 
    url(r'^payments/charge', views.PaymentsChargeView.as_view(), name='charge'), 
] 
+1

請使用[編輯]鏈接來解釋此代碼如何工作,而不只是給代碼,因爲解釋更有可能幫助未來的讀者。另見[回答]。 [源](http://stackoverflow.com/users/5244995) –