21

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

5 Answers 5

69

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You are right. I faced the same error for my class based view. I was using the @csrd-exempt decorator and it showed the Attribute Error. +1!
Normally I wouldn't approve of this, but you just saved me a headache, so I don't care right now. ^_^
I was using @crsf_exempt decorator on the class, and I got this error.. +1 for this one!
Same here, I was trying to apply the @login_required decorator to the CBV
29

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.

1 Comment

Thank you! I had hoped it was just me looking at the code too much and then not being able to see the problem.
29

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)

4 Comments

Thanks for this, and also to Tim Saylor. Exactly what I was looking for.
god bless people like you
3 and a half years after you posted this it has just saved my bacon! Cheers venkat!
Tim Saylor's answer is correct, but I wasn't sure how to implement what he was talking about. This quickly showed me where to method_decorator which fixed my problem.
9

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

2 Comments

Thanks. I also encountered this problem. Actually, I had no need of that decorator.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.