码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2019-10-11 14:07:54      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:col   pre   ini   mini   djang   date   lis   ram   模块   

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

 

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

标签:col   pre   ini   mini   djang   date   lis   ram   模块   

原文地址:https://www.cnblogs.com/lovershowtime/p/11653779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!