Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework: 'function' object has no attribute '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'^$', 'interface.views.index'),
    url(r'^testview/$', TestView.as_view()),
    url(r'^people/$', ListOrCreateModelView.as_view(resource=PersonResource)),
)

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

like image 891
Aaron Avatar asked Jul 27 '11 02:07

Aaron


2 Answers

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.

like image 90
Tim Saylor Avatar answered Nov 10 '22 22:11

Tim Saylor


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.

like image 29
Ismail Badawi Avatar answered Nov 10 '22 23:11

Ismail Badawi