我一直在浏览Django Rest Framework的教程,以构建一个简单的待办事项列表应用程序。
当我从基于函数的视图转换到基于类的视图时,我开始获取到Django的堆栈跟踪,并且所有http调用都开始失败。
我使用virtualenv
来隔离依赖项,并使用Python 3.5.2。
任何人对于发生了什么或如何解决此错误有任何想法?
我将此问题归咎于配置错误或版本不匹配,但我不知道从哪里开始。
/views.py:
from django.http import Http404
from list.models import List
from list.serializers import ListSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
class ListsView(APIView):
"""
List all lists.
"""
def get(self, request, format=None):
lists = List.objects.all()
serializer = ListSerializer(lists, many=True)
return Response(serializer.data)
...
/urls.py
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from list import views
urlpatterns = [
url(r'^lists/$', views.ListsView),
url(r'^lists/(?P<pk>[0-9]+)/$', views.ListView),
url(r'^lists/(?P<list>[0-9]+)/items/$', views.ListItemsView),
url(r'^lists/(?P<list>[0-9]+)/items/(?P<pk>[0-9]+)/$', views.ListItemView),
]
urlpatterns = format_suffix_patterns(urlpatterns)
输入:
curl http://localhost:8000/lists/
跟踪:
Internal Server Error: /lists/
Traceback (most recent call last):
File "<pwd>/env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "<pwd>/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "<pwd>/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "<pwd>/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
[29/Aug/2016 00:03:32] "GET /lists/ HTTP/1.1" 500 61615
ENV:
python 3.5.2
要求:
Django==1.10
django-lint==2.0.4
djangorestframework==3.4.6
logilab-astng==0.24.3
logilab-common==1.2.2
psycopg2==2.6.2
Pygments==2.1.3
pylint==0.28.0
six==1.10.0
答案 0 :(得分:0)
@SergeyGornostaev有正确的直觉来查看并发布urls.py
。
在从基于函数的视图到基于类的视图的重构中,当我更新路由时,我忽略了对视图调用as_view()
。
之前:url(<pattern>, views.ListsView)
之后:url(<pattern>, views.ListView.as_view())
这解决了错误。
之前的完整/urls.py:
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from list import views
urlpatterns = [
url(r'^lists/$', views.ListsView),
url(r'^lists/(?P<pk>[0-9]+)/$', views.ListView),
url(r'^lists/(?P<list>[0-9]+)/items/$', views.ListItemsView),
url(r'^lists/(?P<list>[0-9]+)/items/(?P<pk>[0-9]+)/$', views.ListItemView),
]
urlpatterns = format_suffix_patterns(urlpatterns)
完整的/urls.py之后:
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from list import views
urlpatterns = [
url(r'^lists/$', views.ListsView.as_view()),
url(r'^lists/(?P<pk>[0-9]+)/$', views.ListView.as_view()),
url(r'^lists/(?P<list>[0-9]+)/items/$', views.ListItemsView.as_view()),
url(r'^lists/(?P<list>[0-9]+)/items/(?P<pk>[0-9]+)/$', views.ListItemView.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)