标签:turn int before object 搜索 UNC 通过 csr 复数
CBV:基于类的视图
urls.py
from django.urls import path
from app01.views import StudentsView
urlpatterns = [
path('students/ ',StudentsView.as_view()),
]
views.py
from django.views import View
from django.shortcuts import HttpResponse
class StudentsView(View):
def get(self,request,*args,**kwargs):
return HttpResponse('GET 请求')
def post(self,request,*args,**kwargs):
return HttpResponse('POST 请求')
csrf_exempt
csrf_protect
# 第一种:给方法加
from django.views import View
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from django.utils.decorators import method_decorator
class StudentsView(View):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(StudentsView, self).dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
print('GET')
return HttpResponse('GET请求')
def post(self, request, *args, **kwargs):
return HttpResponse('POST请求')
# 第二种:给类加
@method_decorator(csrf_exempt,name='dispatch')
class TeachersView(View):
def get(self, request, *args, **kwargs):
print('GET')
return HttpResponse('GET请求')
def post(self, request, *args, **kwargs):
return HttpResponse('POST请求')
class StudentsView(View):
def dispatch(self, request, *args, **kwargs):
# return HttpResponse('dispath')
func = getattr(self,request.method.lower())
return func(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
return HttpResponse('GET请求')
def post(self, request, *args, **kwargs):
return HttpResponse('POST请求')
def delete(self,request,*args,**kwargs):
return HttpResponse('DELETE请求')
def put(self,request,*args,**kwargs):
return HttpResponse('PUT请求')
class MyBaseView(object):
def dispatch(self, request, *args, **kwargs):
print('before')
func = super(MyBaseView, self).dispatch(request, *args, **kwargs)
print('after')
return func
class StudentsView(MyBaseView, View):
def get(self, request, *args, **kwargs):
print('GET')
return HttpResponse('GET请求')
def post(self, request, *args, **kwargs):
return HttpResponse('POST请求')
def delete(self, request, *args, **kwargs):
return HttpResponse('DELETE请求')
def put(self, request, *args, **kwargs):
return HttpResponse('PUT请求')
from django.urls import path
from app01.views import OrderView
urlpatterns = [
path('order/',OrderView.as_view()),
]
class OrderView(View):
def get(self, request, *args, **kwargs):
return HttpResponse('获取订单')
def post(self, request, *args, **kwargs):
return HttpResponse('创建订单')
def delete(self, request, *args, **kwargs):
return HttpResponse('删除订单')
def put(self, request, *args, **kwargs):
return HttpResponse('更新订单')
认证
```python
from rest_framework.views import APIView
from rest_framework.exceptions import AuthenticationFailed
class MyAuthentication(object):
def authenticate(self, request):
token = request._request.GET.get('token')
# 可以做用户名密码验证
if not token:
raise AuthenticationFailed('认证失败')
return (1, 2)
def authenticate_header(self, request):
pass
class DogView(APIView):
authentication_classes = [MyAuthentication]
def get(self, request, *args, **kwargs):
return HttpResponse('获取小狗')
def post(self, request, *args, **kwargs):
return HttpResponse('创建小狗')
def delete(self, request, *args, **kwargs):
return HttpResponse('删除小狗')
def put(self, request, *args, **kwargs):
return HttpResponse('更新小狗')
```
标签:turn int before object 搜索 UNC 通过 csr 复数
原文地址:https://www.cnblogs.com/xjmlove/p/10394796.html