Django Rest 框架:“函数”对象没有属性“as_view”;
我已经尝试使用 Django Rest Framework 让 ModelResource 或 View 工作一段时间了。我正在遵循示例,但示例中的代码不适合我。谁能告诉我为什么我可能会收到此错误。
view.py
# Create your views here.
from django.http import HttpResponse
from django.utils import simplejson
from django.core import serializers
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status
from interface.models import *
def TestView(View):
def get(self, request):
return Person.objects.all()
urls.py
from django.conf.urls.defaults import *
from djangorestframework.resources import ModelResource
from djangorestframework.views import ListOrCreateModelView, InstanceModelView, View
from interface.models import *
from interface.views import *
class PersonResource(ModelResource):
model = Person
ordering = ('LastName')
urlpatterns = patterns('',
url(r'^$', 'interface.views.index'),
url(r'^testview/$', TestView.as_view()),
url(r'^people/$', ListOrCreateModelView.as_view(resource=PersonResource)),
)
我现在收到错误“function”对象没有属性“as_view”。
I've been trying for a while to get a ModelResource or a View working using the Django Rest Framework. I'm following the examples but the code in the examples is not working for me. Can anyone tell me why I might be getting this error.
views.py
# Create your views here.
from django.http import HttpResponse
from django.utils import simplejson
from django.core import serializers
from djangorestframework.views import View
from djangorestframework.response import Response
from djangorestframework import status
from interface.models import *
def TestView(View):
def get(self, request):
return Person.objects.all()
urls.py
from django.conf.urls.defaults import *
from djangorestframework.resources import ModelResource
from djangorestframework.views import ListOrCreateModelView, InstanceModelView, View
from interface.models import *
from interface.views import *
class PersonResource(ModelResource):
model = Person
ordering = ('LastName')
urlpatterns = patterns('',
url(r'^
I'm now getting the error 'function' object has no attribute 'as_view'.
, 'interface.views.index'),
url(r'^testview/
I'm now getting the error 'function' object has no attribute 'as_view'.
, TestView.as_view()),
url(r'^people/
I'm now getting the error 'function' object has no attribute 'as_view'.
, ListOrCreateModelView.as_view(resource=PersonResource)),
)
I'm now getting the error 'function' object has no attribute 'as_view'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于这是 google 上此错误消息的排名第一,并且有比 OP 更微妙且可能更常见的原因,因此我在这里发布此评论。
此错误也可能是由于在基于类的视图上使用标准视图装饰器而不是视图中的 __dispatch__ 方法引起的。
Since this is the #1 hit on google for this error message and there's a more subtle and probably common cause for it than the OPs, I'm posting this comment here.
This error can also be caused by using a standard view decorator on a class based view instead of on the
__dispatch__
method within the view.def TestView(View):
应该是class TestView(View):
。按照目前的情况,您定义一个名为TestView
的函数,它接受一个名为View
的参数 - 它的主体定义了一个内部函数,然后返回None
。def TestView(View):
should beclass TestView(View):
. As it stands, you define a function calledTestView
which takes an argument calledView
-- its body defines an inner function, then returnsNone
.要添加 Tim Saylor 观点,
https://docs.djangoproject.com/ en/dev/topics/class-based-views/intro/#id1
To add to Tim Saylor point,
https://docs.djangoproject.com/en/dev/topics/class-based-views/intro/#id1
我也遇到了这个错误,但就我而言,我用以下想法解决了它。
如果您尝试覆盖一个类,通常会发生该错误。如果您复制并粘贴代码并忘记更改例如类名称,有时会发生这种情况。但就我而言,情况略有不同
如果将
@login_required
应用于类,您将收到错误消息:'function' object has no attribute 'as_view'
所以,现在你应该如何装饰 Django 中的类?
对于基于类的视图,您有两种装饰类的选项。
1) 修饰 URLconf
2) 修饰类
这两个选项都会产生相同的结果 - 仅限制登录用户对类的访问。选项之间的区别在于装饰器如何应用于类实例。有关装饰器实现,请参阅此页面
https://docs.djangoproject.com/en/1.4/topics/class-based-views/#decorating-class-based-views
I am also getting this error but in my case i solved it with following idea.
That error usually happens if you try to override a class. That sometimes happens if you copy&paste code and forget to change e.g. the class name. But in my case it was little different
If you apply
@login_required
to a class, you will receive the error message:‘function’ object has no attribute ‘as_view’
So, how should you decorate classes in Django now?
For class-based views, you have two options of decorating your classes.
1) Decorating the URLconf
2) Decorating the class
Both options leads to the same result - restricting the access to a class only for logged users. The difference between the options is how the decorator is applied to the class instance.Refer this page for decorators implementation
https://docs.djangoproject.com/en/1.4/topics/class-based-views/#decorating-class-based-views
如果您使用装饰器应该使用的类,请使用它
先导入:
然后
use this if you use a class your decorator should be
import first:
then