0

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})
Roil.Li
  • 31
  • 3

2 Answers2

1

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

Aayush Agrawal
  • 1,354
  • 1
  • 12
  • 24
1

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})
minglyu
  • 2,958
  • 2
  • 13
  • 32
  • one element tuple requires comma, read more here https://stackoverflow.com/questions/7992559/what-is-the-syntax-rule-for-having-trailing-commas-in-tuple-definitions – minglyu Jul 25 '20 at 09:36