Django Rest 框架:“函数”对象没有属性“as_view”;

发布于 2024-11-26 05:54:37 字数 1149 浏览 2 评论 0原文

我已经尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无所的.畏惧 2024-12-03 05:54:37

由于这是 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.

帥小哥 2024-12-03 05:54:37

def TestView(View): 应该是 class TestView(View):。按照目前的情况,您定义一个名为 TestView 的函数,它接受一个名为 View 的参数 - 它的主体定义了一个内部函数,然后返回 None

def TestView(View): should be class TestView(View):. As it stands, you define a function called TestView which takes an argument called View -- its body defines an inner function, then returns None.

岁月静好 2024-12-03 05:54:37

要添加 Tim Saylor 观点,

https://docs.djangoproject.com/ en/dev/topics/class-based-views/intro/#id1

要装饰基于类的视图的每个实例,您需要装饰
类定义本身。为此,您将装饰器应用到
类的dispatch()方法。

类上的方法与独立函数不太一样,所以
你不能只将函数装饰器应用于方法 - 你需要
首先将其转换为方法装饰器。方法装饰器
装饰器将函数装饰器转换为方法装饰器,因此
它可以用在实例方法上。例如:

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView

class ProtectedView(TemplateView):
    template_name = 'secret.html'

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(ProtectedView, self).dispatch(*args, **kwargs)

To add to Tim Saylor point,

https://docs.djangoproject.com/en/dev/topics/class-based-views/intro/#id1

To decorate every instance of a class-based view, you need to decorate
the class definition itself. To do this you apply the decorator to the
dispatch() method of the class.

A method on a class isn’t quite the same as a standalone function, so
you can’t just apply a function decorator to the method – you need to
transform it into a method decorator first. The method_decorator
decorator transforms a function decorator into a method decorator so
that it can be used on an instance method. For example:

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView

class ProtectedView(TemplateView):
    template_name = 'secret.html'

    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(ProtectedView, self).dispatch(*args, **kwargs)
丘比特射中我 2024-12-03 05:54:37

我也遇到了这个错误,但就我而言,我用以下想法解决了它。

如果您尝试覆盖一个类,通常会发生该错误。如果您复制并粘贴代码并忘记更改例如类名称,有时会发生这种情况。但就我而言,情况略有不同

如果将 @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

壹場煙雨 2024-12-03 05:54:37

如果您使用装饰器应该使用的类,请使用它
先导入:

from django.utils.decorators import method_decorator

然后

@method_decorator(login_required(login_url='login'),name="dispatch")
class YourClassView(YourView):

use this if you use a class your decorator should be
import first:

from django.utils.decorators import method_decorator

then

@method_decorator(login_required(login_url='login'),name="dispatch")
class YourClassView(YourView):
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文