reverse(两种反向生成url django原生形式和rest_framework中版本的形式)

reverse(两种反向生成url django原生形式和rest_framework中版本的形式)

views.py

from django.shortcuts import render,HttpResponse
from rest_framework.views import APIView

from django.urls import reverse
from rest_framework.request import Request
# Create your views here.

class UsersView(APIView):
    #反向生成URL
    def get(self,request, *args, **kwargs):
        print(request.version)
        u1 = request.versioning_scheme.reverse(viewname='user',request=request)
        print(u1)
        u2 = reverse(viewname='user',kwargs={'version':1})
        print(u2)
        return HttpResponse('用户列表')

urls.py
from django.conf.urls import url,include
from cmdb import views
urlpatterns = [
    url(r'^(?P<version>[v1|v2]+)/users/$',views.UsersView.as_view(),name='user')
]

 

posted @ 2018-07-17 23:02  想翻身的猫  阅读(258)  评论(0编辑  收藏  举报