加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-20 10:20:50 所属栏目:Python 来源:网络整理
导读:一. 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

一. 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(rusers,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)) ]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读