[英]Class-based view “has no attribute .as_view()” error
I'm following this tutorial , trying to make an API for my Products
table. 我正在学习本教程 ,试图为我的
Products
表创建一个API。
Here's my .views/API/apitest.py view: 这是我的.views / API / apitest.py视图:
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: urls.py条目:
url(r'^API/products/$', views.API.apitest.as_view(), name='apitest')
That line gives an error: 'module' object has no attribute 'as_view'
. 该行给出错误:
'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.
我正在使用Django 1.9.2。
apitest is the module, you need to use as_view
on the class apitest是模块,你需要在类上使用
as_view
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.