api/序列化器.py

api/序列化器.py
File "/home/Desktop/booklib/env/lib/python3.8/site-packages/rest_framework/authentication.py", line 126, in authenticate
    self.enforce_csrf(request)
  File "/home/Desktop/booklib/env/lib/python3.8/site-packages/rest_framework/authentication.py", line 135, in enforce_csrf
    check = CSRFCheck()
TypeError: __init__() missing 1 required positional argument: 'get_response'
[21/Dec/2021 14:37:05] "GET /api/ HTTP/1.1" 500 112898

api/序列化器.py


from book.models import Book


class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ('title', 'author','summary', 'isbn')```



** Create your views here :api/views.py**


```from django.shortcuts import render
from rest_framework import generics

from book.models import Book
from .serializers import BookSerializer


class BookAPIView(generics.ListAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer```


**# api/urls.py**
```from django.urls import path
from .views import BookAPIView

urlpatterns = [
    path('', BookAPIView.as_view())
]```

答案1

我在升级到 django 4.0 时也遇到了同样的问题。将rest_framework升级到最新版本(https://github.com/encode/django-rest-framework/releases) 修复。

相关内容