Django python manage.py runserver返回属性错误,而不是启动虚拟服务器

2024-10-01 09:25:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我多次查看这些行,并在网站上搜索以找到类似的问题,但我无法找出我遗漏了什么导致了错误

url.py-profiles\u api

from django.urls import path
from profiles_api import views

urlpatterns = [
    path('hello-view/', views.HelloApiView.as_View()),
]

views.py-profiles\u api

from rest_framework.views import APIView
from rest_framework.response import Response

class HelloApiView(APIView):
    """test API View"""

    def get(self, request, format=None):
        """Returns a list of APIView features"""
        an_apiview = [
            'Uses HTTP methods as function (get, post, patch, put, delete)',
            'Is similar to a traditional Django View',
            'Gives you the most control over your application logic',
            'Is mapped manually to URLs',
        ]

        return Response({'message': 'Hello!', 'an_apiview': an_apiview})

url.py-profiles\u项目

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('profiles_api.urls'))
]

例外情况:

(env) vagrant@ubuntu-bionic:/vagrant$ python manage.py runserver 0.0.0.0:8080

....

path('hello-view/', views.HelloApiView.as_View()),

AttributeError: type object 'HelloApiView' has no attribute 'as_View'


Tags: pathdjangofrompyimportanviewapi