Django Rest Framework:无法克服奇怪的错误 [英] Django Rest Framework: Can't get over strange error
问题描述
尝试一个简单的请求:
urls.py
from django.conf.urls import url
from django.urls import include, path
from rest_framework import routers
from django.http import HttpResponse
from rest_framework.urlpatterns import format_suffix_patterns
from .public_views import NavigationBar
router = routers.DefaultRouter()
router.register(r'navbar', NavigationBar, basename="NavigationBar")
urlpatterns = [
path('', include(router.urls))
]
urlpatterns = format_suffix_patterns(urlpatterns)
public_views.py
from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny
from rest_framework.throttling import UserRateThrottle
from rest_framework.decorators import api_view, throttle_classes
from . view_utils import *
class OncePerDayUserThrottle(UserRateThrottle):
rate = '1/day'
class NavigationBar(APIView):
"""
obtain up to date navigation bar (or side menu navigation) hierarchy.
"""
permission_classes = ([AllowAny])
def get(self, request, format=None):
"""
get user addresses
"""
return Response("this is a good response")
def get_extra_actions(cls):
return []
当我API调用/v1/navbar
或/v1/navbar/
端点时(我确实有主要的 urls.py
线索)所有/v1/
流量都传输到另一个专用 urls.py
),我收到以下错误消息:
When I API call the /v1/navbar
or /v1/navbar/
endpoints (I do have my main urls.py
lead all /v1/
traffic to another dedicated urls.py
), I am getting the following error:
AttributeError at /v1/navbar
type object 'NavigationBar' has no attribute 'get_extra_actions'
Request Method: GET
Request URL: http://web/v1/navbar
Django Version: 2.1
Exception Type: AttributeError
Exception Value:
type object 'NavigationBar' has no attribute 'get_extra_actions'
Exception Location: /usr/local/lib/python3.6/site-packages/rest_framework/routers.py in get_routes, line 200
Python Executable: /usr/local/bin/uwsgi
Python Version: 3.6.8
Python Path:
['.',
'',
'/usr/local/lib/python36.zip',
'/usr/local/lib/python3.6',
'/usr/local/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6/site-packages']
Server time: Tue, 2 Jul 2019 17:12:27 +0000
任何指针,我将不胜感激.另外,我无法理解为什么错误消息中包含请求URL:http://web/v1/navbar
指示,当 web
不属于我的URL时使用. web
来自哪里???我只是使用/v1/navbar/
击中端点.
I would appreciate any pointers. Also, I fail to understand why the error message includes a Request URL: http://web/v1/navbar
indication when web
is not part of the URL I'm using. Where is web
coming from??? I just use /v1/navbar/
to hit the endpoint.
推荐答案
此处有两处错误.首先,路由器用于视图集,而不是简单视图.其次,对于基于类的视图,您需要通过urlconf中的 as_view()
方法来调用它.因此,摆脱掉路由器的东西,然后做:
There are two things wrong here. First, routers are for viewsets, not simple views. Secondly, with a class based view you need to call it via its as_view()
method in the urlconf. So get rid of that router stuff and just do:
urlpatterns = [
path(r'navbar', NavigationBar.as_view(), name="NavigationBar")
]
注意,现在您不使用路由器,根本不需要该 get_extra_actions
方法.
Note, now you're not using a router, you don't need that get_extra_actions
method at all.
这篇关于Django Rest Framework:无法克服奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!