运维开发网

八.Django---framework框架 url控制

运维开发网 https://www.qedev.com 2020-07-17 07:56 出处:网络 作者:运维开发网整理
一. url控制 https://www.cnblogs.com/changwenjun-666/p/11140892.html    http://www.luyixian.cn/news_show_103564.aspx

一. url控制

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‘^authors/(?P<pk>\d+)/$‘, views.AuthorModelView.as_view({"get":"retrieve","put":"update","delete":"destroy"}),name="detailauthor"), url(r‘‘, include(routers.urls)), 这中路由控制要先注册 自动生成 等同上面路由 url(r‘^login/$‘, views.LoginView.as_view(),name="login"), ]

 

我们现在准备创建我们的API。这是我们项目的根urls.py模块: https://www.django-rest-framework.org/

from django.conf.urls import url, include from django.contrib.auth.models import User from rest_framework import routers, serializers, viewsets # Serializers define the API representation. class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = [‘url‘, ‘username‘, ‘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‘)) ]

0

精彩评论

暂无评论...
验证码 换一张
取 消