Eight .Django --- framework url control framework

A. Url control

https://www.cnblogs.com/changwenjun-666/p/11140892.html   

http://www.luyixian.cn/news_show_103564.aspx

https://blog.csdn.net/weixin_42625143/article/details/96993629

from django.conf.urls import url,include
from django.contrib import admin

from rest_framework import routers
from app01 import views



routers=routers.DefaultRouter()
routers.register("authors",views.AuthorModelView)


urlpatterns = [



    url(r'^admin/', admin.site.urls),
    url(r'^publishes/$', views.PublishView.as_view(),name="publish"), #  View:view(request)=====APIView:dispatch()
    url(r'^publishes/(?P<pk>\d+)/$', views.PublishDetailView.as_view(),name="detailpublish"), #  View:view(request)=====APIView:dispatch()

    url(r'^books/$', views.BookView.as_view(),name="books"),
    url(r'^books/(\d+)/$', views.BookDetailView.as_view(),name="detailbook"),

#url(r'^books/(\d+)/$', View:view), # view(request) # url(r'^authors/$', views.AuthorModelView.as_view({"get":"list","post":"create"}),name="author"), # URL (R & lt '? ^ The authors / (P <PK> \ + D) / $', views.AuthorModelView.as_view ({ "GET": "Retrieve", "PUT": "Update", "Delete": "the destroy "}), name =" detailauthor ") , URL (R & lt '' , the include (routers.urls)), which in the first register automatically generate routing control equivalent to the above route URL (R & lt ' ^ Login / $ ' , views.LoginView .as_view (), name = " Login " ), ]

 

We are now ready to create our API. This is the root of our project urls.py module:
 https://www.django-rest-framework.org/
from django.conf.urls Import url, the include from django.contrib.auth.models Import the User from rest_framework Import Routers, serializers, viewsets # serializers The DEFINE the API representation. class UserSerializer (serializers.HyperlinkedModelSerializer): class Meta -: Model = the User Fields = [ ' URL ' , ' username ' , ' In Email', 'is_staff'] # ViewSets define the view behavior. class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer # Routers provide an easy way of automatically determining the URL conf. router = routers.DefaultRouter() router.register(r'users', UserViewSet) # Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API. urlpatterns = [ url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]

 

Guess you like

Origin www.cnblogs.com/lovershowtime/p/11653779.html
Recommended