【发布时间】:2020-11-11 00:52:38
【问题描述】:
我是 Django 的新手。当我在 url "localhost:8000/api/questions/" 中访问可浏览 api 时,我得到 TypeError 'str' object is not callable in my Django Project。我正在使用 Django 3。我不知道如何调试此错误所以我在这里写。我认为我在 urls
上做错了什么主 urls.py
path('accounts/',
include('django.contrib.auth.urls')
),
path('accounts/',
include('django_registration.backends.one_step.urls')
),
path('api/',
include('users.api.urls')
),
path('api/',
include('questions.api.urls')
)
urls.py 在应用中
from rest_framework.routers import DefaultRouter
from questions.api.views import QuestionViewSet
router = DefaultRouter()
router.register(r'questions', QuestionViewSet)
urlpatterns = [
path('', include(router.urls))
]
views.py 在应用中
from rest_framework.permissions import IsAuthenticated
from questions.api.permissions import IsAuthorOrReadOnly
from questions.api.serializers import QuestionSerializer, AnswerSerializer
from questions.models import Question
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)
serializers.py 在应用中
from rest_framework import serializers
from questions.models import Answer, Question
class QuestionSerializer(serializers.ModelSerializer):
author = serializers.StringRelatedField(read_only=True)
created_at = serializers.SerializerMethodField()
slug = serializers.SlugField(read_only=True)
answer_count = serializers.SerializerMethodField()
user_has_answered = serializers.SerializerMethodField()
class Meta:
model = Question
exclude = ['updated_at']
def get_created_at(self, instance):
return instance.created_at.strftime('%B %d, %Y')
def get_answer_count(self, instance):
return instance.answers.count()
def get_user_has_answered(self, instance):
request = self.context.get('request')
return instance.answers.filter(author=request.user.pk).exists()
我的项目结构
/project
/questions
/api
serializers.py
views.py
urls.py
错误回溯
TypeError at /api/questions/
'str' object is not callable
Request Method: GET
Request URL: http://localhost:8000/api/questions/
Django Version: 3.0.8
Exception Type: TypeError
Exception Value:
'str' object is not callable
Exception Location: /home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in <listcomp>, line 278
Python Executable: /home/myrza/Desktop/ff/django/udemyfinpr/venv/bin/python
Python Version: 3.6.9
Python Path:
['/home/myrza/Desktop/ff/django/udemyfinpr/QuestionTime',
'/usr/lib/python36.zip',
'/usr/lib/python3.6',
'/usr/lib/python3.6/lib-dynload',
'/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages']
Server time: Tue, 21 Jul 2020 15:05:47 +0000
Traceback Switch to copy-and-paste view
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/django/core/handlers/exception.py in inner
response = get_response(request) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = self.process_exception_by_middleware(e, request) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/django/core/handlers/base.py in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py in wrapped_view
return view_func(*args, **kwargs) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/viewsets.py in view
return self.dispatch(request, *args, **kwargs) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
response = self.handle_exception(exc) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in handle_exception
self.raise_uncaught_exception(exc) …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in raise_uncaught_exception
raise exc …
▶ Local vars
/home/myrza/Desktop/ff/django/udemyfinpr/venv/lib/python3.6/site-packages/rest_framework/views.py in dispatch
self.initial(request, *args, **kwargs)
【问题讨论】:
-
添加完整的错误回溯
标签: python django django-rest-framework django-urls