基于类的视图“没有属性.as_view()".错误 [英] Class-based view "has no attribute .as_view()" error

查看:49
本文介绍了基于类的视图“没有属性.as_view()".错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注此教程,尝试为我的 Products 表创建API.

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

这是我的.views/API/apitest.py视图:

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

urls.py条目:

The urls.py entry:

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

该行给出了错误:'module'对象没有属性'as_view'.目前,我只是想创建一个简单的示例,因此不需要装饰器.是什么导致此错误?我正在使用Django 1.9.2.

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是模块,您需要在该类上使用 as_view

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

这篇关于基于类的视图“没有属性.as_view()".错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆