我試圖創建一個API視圖,但出現錯誤。誰能幫忙?'str'對象不可調用Django Rest框架
urls.py:
app_name = 'ads'
urlpatterns = [
# ex: /ads/
url(r'^$', views.ListBrand.as_view(), name='brand_list'),
]
views.py:
from rest_framework.views import APIView
from rest_framework.response import Response
from . import models
from . import serializers
class ListBrand(APIView):
def get(self, request, format=None):
brands = models.Brand.objects.all()
serializer = serializers.BrandSerializer(brands, many=True)
data = serializer.data
return Response(data)
UPDATE:以下是錯誤,它是一個字符串錯誤。我似乎無法找到它來自哪裏。
TypeError at /api/v1/ads/
'str' object is not callable
Request Method: GET
Request URL: http://localhost/api/v1/ads/
Django Version: 1.10.2
Exception Type: TypeError
Exception Value:
'str' object is not callable
Exception Location: C:\Users\Leon\Desktop\esriom\lib\site-packages\rest_framework\views.py in <listcomp>, line 264
Python Executable: C:\Users\Leon\Desktop\esriom\Scripts\python.exe
Python Version: 3.5.2
Python Path:
['C:\\Users\\Leon\\Desktop\\esirom',
'C:\\Users\\Leon\\Desktop\\esriom\\lib\\site-packages\\setuptools-18.1-py3.5.egg',
'C:\\Users\\Leon\\Desktop\\esriom\\lib\\site-packages\\pip-7.1.0-py3.5.egg',
'C:\\Users\\Leon\\Desktop\\esriom\\Scripts\\python35.zip',
'C:\\Users\\Leon\\AppData\\Local\\Programs\\Python\\Python35-32\\DLLs',
'C:\\Users\\Leon\\AppData\\Local\\Programs\\Python\\Python35-32\\lib',
'C:\\Users\\Leon\\AppData\\Local\\Programs\\Python\\Python35-32',
'C:\\Users\\Leon\\Desktop\\esriom',
'C:\\Users\\Leon\\Desktop\\esriom\\lib\\site-packages']
Server time: Fri, 7 Oct 2016 12:44:04 -0500
這裏有TRACEBACK TOO
Environment:
Request Method: GET
Request URL: http://localhost/api/v1/ads/
Django Version: 1.10.2
Python Version: 3.5.2
Installed Applications:
['rest_framework',
'ads.apps.AdsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\django\views\generic\base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\rest_framework\views.py" in dispatch
457. request = self.initialize_request(request, *args, **kwargs)
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\rest_framework\views.py" in initialize_request
364. authenticators=self.get_authenticators(),
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\rest_framework\views.py" in get_authenticators
264. return [auth() for auth in self.authentication_classes]
File "C:\Users\Leon\Desktop\esriom\lib\site-packages\rest_framework\views.py" in <listcomp>
264. return [auth() for auth in self.authentication_classes]
Exception Type: TypeError at /api/v1/ads/
Exception Value: 'str' object is not callable
你什麼錯誤? – 1GDST
你讓我們猜猜錯誤發生在哪裏。請發佈整個錯誤追溯,包括實際發生錯誤的代碼行。 –
你的模型中有'__str __(self)'函數嗎?像這樣:https://docs.djangoproject.com/en/1.10/ref/models/instances/#str – 1GDST