简体   繁体   中英

Class-based view “has no attribute .as_view()” error

I'm following this tutorial , trying to make an API for my Products table.

Here's my .views/API/apitest.py view:

from my_app.views.API.serializers import ProductSerializer
from my_app.models import Product
from rest_framework import generics

class APITest(generics.ListAPIView):
    model=Product
    serializer_class=ProductSerializer
    queryset = Product.objects.all()

The urls.py entry:

url(r'^API/products/$', views.API.apitest.as_view(), name='apitest')

That line gives an error: 'module' object has no attribute 'as_view' . I'm just trying to create a simple example for the moment, so there's no need for decorators. What causes this error? I'm using Django 1.9.2.

apitest is the module, you need to use as_view on the class

url(r'^API/products/$', views.API.apitest.APITest.as_view(), name='apitest')

Although it may be better to look into your imports

from myapp.views.API.apitest import APITest
url(r'^API/products/$', APITest.as_view(), name='apitest')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM