api 链接不起作用。显示此错误“ init () 接受 1 个位置参数,但给出了 2 个”
请帮我。我是 django 的初学者
尝试创建博客 api model.py:
class Blog(models.Model):
blog_id = models.AutoField(primary_key=True, editable=False)
title = models.CharField(max_length=100, null=False)
description = models.TextField(null=False)
image = models.ImageField(null=True, blank=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.title
视图.py
class BlogViewSet(viewsets.ModelViewSet):
serializer_class = BlogSerializer
permission_classes = IsAuthenticated
queryset = Blog.objects.all()
def perform_create(self, serializer):
serializer.save(user=self.request.user)
序列化程序.py:
class BlogSerializer(serializers.ModelSerializer):
user = serializers.SerializerMethodField(read_only=True)
class Meta:
model = Blog
fields = '__all__'
网址.py
from django.urls import path
from rest_framework.routers import DefaultRouter
from . import views
from rest_framework_simplejwt.views import (
TokenObtainPairView,
)
router = DefaultRouter()
router.register('blogs', views.BlogViewSet, basename='blogs')
urlpatterns = [
path('users/login', views.MyTokenObtainPairView.as_view()),
path('users/register', views.register_user, name='register'),
path('users/profile', views.get_user_profile, name='profile'),
path('blogs/', views.BlogViewSet, name='blogs'),
path('avatar/', views.get_avatar, name='avatar'),
]
因此,当我对“http://localhost:8000/api/blogs/”执行获取请求时,它会显示此错误:
TypeError at /api/blogs/
__init__() takes 1 positional argument but 2 were given
Request Method: GET
Request URL: http://localhost:8000/api/blogs/
Django Version: 3.2.4
Exception Type: TypeError
Exception Value:
__init__() takes 1 positional argument but 2 were given
Exception Location: D:\work environment\Django_Api\codersavvy\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
Python Executable: D:\work environment\Django_Api\codersavvy\Scripts\python.exe
Python Version: 3.9.2
Python Path:
['D:\\work environment\\Django_Api\\codersavvy',
'C:\\Users\\S\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\S\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\S\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\S\\AppData\\Local\\Programs\\Python\\Python39',
'D:\\work environment\\Django_Api\\codersavvy',
'D:\\work environment\\Django_Api\\codersavvy\\lib\\site-packages']
Server time: Mon, 14 Jun 2021 17:28:12 +0000