我正在关注Django-Restful教程here,我编写了以下代码
在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)
在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)
当我运行服务器时,我可以获得正常的http://127.0.0.1:8000/snippets/来返回标准的JSON响应,但是当我尝试访问时
http http://127.0.0.1:8000/snippets.json # JSON suffix
http http://127.0.0.1:8000/snippets.api # Browsable API suffix
我收到一个产生此追溯的错误:
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'
为什么我收到此错误?
修改:指向settings.py
的链接答案 0 :(得分:0)
问题是我在views.py
文件中有重复的函数,其中不同的装饰器靠近底部。删除它们解决了这个问题。
答案 1 :(得分:0)
试试这个
http http://127.0.0.1:8000/snippets.json # JSON suffix http
http://127.0.0.1:8000/snippets.api # Browsable API suffix
而不是
http http://127.0.0.1:8000/snippets/?format=json
http http://127.0.0.1:8000/snippets/?format=api
答案 2 :(得分:0)
我遇到一个类似的问题,我的错误是我在声明URL而不是Django Rest Framework的反向模块时使用的是Django的反向模块:
请勿使用此:
from django.urls import reverse
改为使用此:
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),
})