urls:
from django.conf.urls import url from app01 import views urlpatterns = [ # url(r"comment/$",views.Comment.as_view()), url(r"comment/(?P<pk>\d+)",views.CommentDetailView.as_view()), url(r"article/$",views.Article.as_view()), url(r"school/(?P<id>\d+)",views.SchoolDetail.as_view(),name="school-detail"), url(r"school_view/$",views.SchoolView.as_view(),name="school_view-detail"), url(r"comment_view/$",views.CommentView.as_view(),name="comment_view-detail"), url(r"article/(?P<pk>\d+)",views.ArticleDetail.as_view(),name="article-detail"), # url(r‘comment/$‘,views.CommentViewSet.as_view({ # "get":"list", # "put":"create", # })), # # url(r‘comment/(?P<pk>\d+)/$‘,views.CommentViewSet.as_view({ # "get":"retrieve", # "put":"update", # "delete":"destroy" # })) ] from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register(r‘comment‘,views.CommentViewSet) urlpatterns += router.urls # 相当于 在urlpatterns里添加了: # url(r‘comment/$‘,views.CommentViewSet.as_view({ # "get":"list", # "put":"create", # })), # # url(r‘comment/(?P<pk>\d+)/$‘,views.CommentViewSet.as_view({ # "get":"retrieve", # "put":"update", # "delete":"destroy" # }))
views:此处为RESTful API 终极版:
from django.shortcuts import render,HttpResponse from django.http import JsonResponse from app01 import models from rest_framework import serializers from django import views from rest_framework.views import APIView from rest_framework.response import Response from app01 import serializers as app01_serializers from rest_framework.generics import GenericAPIView from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin from rest_framework.generics import GenericAPIView, RetrieveUpdateDestroyAPIView, ListCreateAPIView from rest_framework.viewsets import ModelViewSet from django.conf.urls import url from app01 import views urlpatterns = [ # url(r"comment/$",views.Comment.as_view()), url(r"comment/(?P<pk>\d+)",views.CommentDetailView.as_view()), url(r"article/$",views.Article.as_view()), url(r"school/(?P<id>\d+)",views.SchoolDetail.as_view(),name="school-detail"), url(r"school_view/$",views.SchoolView.as_view(),name="school_view-detail"), url(r"comment_view/$",views.CommentView.as_view(),name="comment_view-detail"), url(r"article/(?P<pk>\d+)",views.ArticleDetail.as_view(),name="article-detail"), # url(r‘comment/$‘,views.CommentViewSet.as_view({ # "get":"list", # "put":"create", # })), # # url(r‘comment/(?P<pk>\d+)/$‘,views.CommentViewSet.as_view({ # "get":"retrieve", # "put":"update", # "delete":"destroy" # })) ] from rest_framework.routers import DefaultRouter router = DefaultRouter() router.register(r‘comment‘,views.CommentViewSet) urlpatterns += router.urls # 相当于 在urlpatterns里添加了: # url(r‘comment/$‘,views.CommentViewSet.as_view({ # "get":"list", # "put":"create", # })), # # url(r‘comment/(?P<pk>\d+)/$‘,views.CommentViewSet.as_view({ # "get":"retrieve", # "put":"update", # "delete":"destroy" # }))
原文地址:https://www.cnblogs.com/stfei/p/9409865.html
时间: 2024-10-11 17:31:56