Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

django

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.

like image 936
Escher Avatar asked Dec 10 '22 17:12

Escher


1 Answers

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')
like image 81
Sayse Avatar answered Dec 13 '22 07:12

Sayse