Django Rest框架:'function'对象没有属性'as_view' [英] Django Rest Framework: 'function' object has no attribute 'as_view'

查看:504
本文介绍了Django Rest框架:'function'对象没有属性'as_view'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试一段时间来获得一个使用Django Rest框架的ModelResource或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'^$', 'interface.views.index'),
    url(r'^testview/$', TestView.as_view()),
    url(r'^people/$', ListOrCreateModelView.as_view(resource=PersonResource)),
)

我现在得到错误'function'对象没有属性'as_view'。

I'm now getting the error 'function' object has no attribute 'as_view'.

推荐答案

def TestView(View):应为 class TestView(View):。您可以定义一个名为 TestView 的函数,该函数接受一个称为 View 的参数 - 其主体定义了内部函数,然后返回

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.

这篇关于Django Rest框架:'function'对象没有属性'as_view'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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