Reputation: 31
I create a basic django rest project and follow the official tutorial.
When I called my API the following error message appears.
Am I missing some settings?
object of type 'type' has no len()
Request Method: GET
Request URL: http://localhost:8000/test
Django Version: 3.0.6
Exception Type: TypeError
Exception Value:
object of type 'type' has no len()
Exception Location: D:\Projects\PythonProjects\poeRadar\venv\lib\site-packages\rest_framework\views.py in default_response_headers, line 158
Python Executable: D:\Projects\PythonProjects\poeRadar\venv\Scripts\python.exe
Python Version: 3.6.5
Here is my code
setting.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
)
}
url.py
from django.conf.urls import url
from backend.api import views
urlpatterns = [
url(r'^test$', views.test)
]
views.py
from rest_framework.response import Response
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.renderers import JSONRenderer
import requests
@api_view(('GET',))
@renderer_classes((JSONRenderer))
def test(request):
return Response({'a': 1})
Upvotes: 0
Views: 346
Reputation: 3337
Seems you are missing a comma in @renderer_classes
just as you did in @api_view
@api_view(('GET',))
@renderer_classes((JSONRenderer,))
def test(request):
return Response({'a': 1})
Upvotes: 1
Reputation: 1394
You can't render a Response object, it's already rendered
Try this:
@api_view(('GET',))
def test(request):
return JsonResponse({'a': 1})
Make sure to import JsonResponse
Upvotes: 1