一. url控制
https://HdhCmsTestcnblogs测试数据/changwenjun-666/p/11140892.html
http://HdhCmsTestluyixian.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://HdhCmsTestdjango-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‘))
]
查看更多关于八.Django---framework框架 url控制的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did170632