Why is there an unexpected keyword argument error for this Django API call?

Related searches

I am following the Django-Restful tutorial here, and I have the following code written

in views.py:

from snippets.models import Snippet                                              
from snippets.serializers import SnippetSerializer                               

from rest_framework import status                                                
from rest_framework.decorators import api_view                                   
from rest_framework.response import Response                                     

@api_view(['GET','POST'])                                                        
def snippet_list(request, format=None):                                          
    """                                                                          
    List all snippets, or create a new snippet.                                  
    """                                                                          
    if request.method == 'GET':                                                  
        snippets = Snippet.objects.all()                                         
        serializer = SnippetSerializer(snippets, many=True)                      
        return Response(serializer.data)                                         
    elif request.method == 'POST':                                               
        serializer = SnippetSerializer(data=request.data)                        
        if serializer.is_valid():                                                
            serializer.save()                                                    
            return Response(serializer.data,status=status.HTTP_201_CREATED)      
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 

in urls.py:

from django.conf.urls import url
from snippets import views
from rest_framework.urlpatterns import format_suffix_patterns

urlpatterns = [
    url(r'^snippets/$',views.snippet_list),
    url(r'^snippets/(?P<pk>[0-9]+)/$',views.snippet_detail)
]

urlpatterns = format_suffix_patterns(urlpatterns)

When I run the server, I can get the normal http://127.0.0.1:8000/snippets/ to return the standard JSON response, but when I try accessing

http http://127.0.0.1:8000/snippets.json  # JSON suffix
http http://127.0.0.1:8000/snippets.api   # Browsable API suffix

I get an error that yields this traceback:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/snippets.json

Django Version: 1.9.7
Python Version: 2.7.6
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'snippets',
 'quickstart']
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.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/myname/workspace/restTutorial/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/myname/workspace/restTutorial/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/myname/workspace/restTutorial/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

Exception Type: TypeError at /snippets.json
Exception Value: snippet_list() got an unexpected keyword argument 'format'

Why am I getting this error?

Edit: link to settings.py

The issue was that I had duplicate functions in the views.py file with different decorators that were near the bottom. Removing them solved the issue.

exception got an unexpected keyword argument 'many' � Issue , encode / django-rest-framework exception got an unexpected keyword argument 'many' #7066 Traceback (most recent call last): File "/usr/local/lib/ python3.7/site-packages/django/ As has been pointed out to you several times, the GitHub issue tracker is intended for bug reports and feature requests. In the current master of channels (also in the version 1.0.2) line 208 of handler.py is response = HttpResponse("408 Request Timeout (upload too slow)", status_code=408) But HttpResponse does not have an argument status_code.

try this

http http://127.0.0.1:8000/snippets.json  # JSON suffix http
http://127.0.0.1:8000/snippets.api   # Browsable API suffix

instead

http http://127.0.0.1:8000/snippets/?format=json  
http http://127.0.0.1:8000/snippets/?format=api

TypeError: __init__() got an unexpected keyword argument , I did the HiperlinkedSerializer tutorial for get the objects child from a object encode / django-rest-framework "GET /music/api/ HTTP/1.1" 500 59 Traceback (most recent call last): File __init__() got an unexpected keyword argument ' view_name' Please send us a small test that produces the error. There's a warning in Django that tells you not to use max_length with IntegerField but at the same time, it's a valid argument - understand, it won't crash if you provide it. Weird. Copy link Quote reply

I ran into a similiar issue my error was that I was using Django's reverse module when declaring the urls instead of the reverse module of Django Rest Framework:

do not use this:

from django.urls import reverse

use this instead:

from rest_framework.reverse import reverse

@api_view(['GET'])
def api_root(request, format=None):
    return Response({
       #'users': reverse('users:user-list', request=request, format=format),
       'transactions': reverse('transactions:transaction-list', request=request, format=format),
})

got an unexpected keyword argument 'max_length', GitHub Login � DjangoProject Login � Preferences � API � View Tickets � Reports closed (invalid). "got an unexpected keyword argument 'max_length'" When I use the Admin App to add a new object of that class, I get the following error:: Traceback (most in _checklogin 55. return view_func(request, *args, **kwargs) File� Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

#4199 (Keyword argument error in serializers) – Django, (this returns with an unexpected keyword argument error for stream however) The simplejson call uses self.options, and stream is not a valid keyword Added 'New in Django development version' to docs/db-api.txt change from [5746]. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Django community: Django Q&A RSS, Django returning "unexpected keyword argument" from another function else: return render(request, "encyclopedia/error.html") the I am building a django project and sending REST API response using� Creating table auth_permission Creating table auth_group Creating table auth_user Creating table auth_message Creating table django_content_type Creating table django_session Creating table django_site Creating table django_admin_log Creating table django_comments Creating table django_comment_flags Creating table tagging_tag Creating table

December 31, 2017 Django Leave a comment. Questions: I am writing a Django application that has a model for People, and I have hit a snag. I am assigning Role objects to people using a Many-To-Many relationship – where Roles have a name

Comments
  • can you post your settings
  • Here is a copy of the file on pastebin: pastebin.com/kvTvaY6V .
  • @user1362215 You need to obscure your secret key when you're posting your settings. And then update the question with that link.
  • Done. Though, this is just a tutorial I am walking through so security is not a huge issue.
  • Please post the full stack trace, not just the last line.