我對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')
謝謝,解決了一個概率 – user1935987
很高興它的工作! –