Escher
Escher

Reputation: 5776

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.

Upvotes: 7

Views: 11094

Answers (1)

Sayse
Sayse

Reputation: 43300

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')

Upvotes: 14

Related Questions