ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – ‘str’对象不可调用Django Rest Framework

2019-07-24 16:57:27  阅读:176  来源: 互联网

标签:python python-3-x django django-rest-framework


我正在尝试创建一个API视图,但我收到了一个错误.有人可以帮忙吗?

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)

更新:这里是错误,这是一个字符串错误.我似乎无法找到它的来源.

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

这里有痕迹

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

解决方法:

我的问题出在我的settings.py文件中:

DIFF:

    REST_FRAMEWORK = {
-       'DEFAULT_AUTHENTICATION_CLASSES': {
+       'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework.authentication.SessionAuthentication',
-        }
+        ),
-        'DEFAULT_PERMISSION_CLASSES': {
+        'DEFAULT_PERMISSION_CLASSES': (
            'rest_framework.permissions.IsAuthenticatedOrReadOnly',
-        },
+        ),
    }

标签:python,python-3-x,django,django-rest-framework
来源: https://codeday.me/bug/20190724/1524316.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有