【发布时间】:2021-08-26 01:47:23
【问题描述】:
当我去这个 http://127.0.0.1:8000/api/questions/ 我得到
/api/questions/ 处的类型错误
'list' 对象不可调用
urls.py
>(在项目中)
"""QuestionTime URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path, re_path
from django_registration.backends.one_step.views import RegistrationView
from core.views import IndexTemplateView
from users.forms import CustomUserForm
# https://django-registration.readthedocs.io/en/3.1.2/activation-workflow.html
urlpatterns = [
path('admin/', admin.site.urls),
path("accounts/register/", RegistrationView.as_view(
form_class=CustomUserForm,
success_url="/",
), name="django_registration_register"),
path("accounts/", include("django_registration.backends.one_step.urls")),
path("accounts/", include("django.contrib.auth.urls")),
path("api/", include("users.api.urls")),
path("api/", include("questions.api.urls")),
path("api-auth/", include("rest_framework.urls")),
path("api/rest-auth/", include("rest_auth.urls")),
path("api/rest-auth/registration/", include("rest_auth.registration.urls")),
re_path(r"^.*$", IndexTemplateView.as_view(), name="entry-point"),
]
urls.py
>(在 appName1/api 中
from django.urls import include, path
from rest_framework import urlpatterns
from rest_framework.routers import DefaultRouter
from questions.api import views as qv
router = DefaultRouter()
router.register(r"questions", qv.QuestionViewSet)
urlpatterns = [
path("", include(router.urls)),
path("questions/<slug:slug>/answers/", qv.AnswerListAPIView.as_view(), name="answer-list"),
path("questions/<slug:slug>/answer/", qv.AnswerCreateAPIView.as_view(), name="answer-create"),
path("answers/<int:pk>/", qv.AnswerRUDAPIView.as_view(), name="answer-detail"),
path("answers/<int:pk>/like/", qv.AnswerLikeAPIView.as_view(), name="answer-like"),
]
views.py
>(在 appName1/api 中) 仅显示 QuestionViewSet
class QuestionViewSet(viewsets.ModelViewSet):
queryset = Question.objects.all()
lookup_field = "slug"
serializer_class = QuestionSerializer
permission_classes = [IsAuthenticated, IsAuthorOrReadOnly]
def perform_create(self, serializer):
serializer.save(author=self.request.user)
来自 settings.py
>REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_PAGINATION_CLASS': (
'rest_framework.pagination.PageNumberPagination',
),
'PAGE_SIZE': 2,
}
错误可能来自其他地方。
我不知道在哪里可以找到“列表”。
appName1 = 问题
异常位置:lib/python3.9/site-packages/rest_framework/generics.py,第 162 行,在分页器中
【问题讨论】:
-
您能否将您的视图集
QuestionViewSet的代码添加到问题中? -
class QuestionViewSet(viewsets.ModelViewSet): queryset = Question.objects.all() lookup_field = "slug" serializer_class = QuestionSerializer permission_classes = [IsAuthenticated, IsAuthorOrReadOnly] def perform_create(self, serializer): serializer.save(author=self.request.user)要在views.py中找到代码,在顶部 -
您发布的代码中的错误在哪里并不明显。您能否将完整的回溯添加到您的问题并删除所有与错误无关的代码,因为它只会增加噪音
-
@IainShelvington 您需要查看哪些文件?
-
错误的完整回溯将是最有用的
标签: python django django-rest-framework typeerror