Naila Iqbal
Naila Iqbal

Reputation: 75

django classbased view not accepting view_args and view_kwargs

I am using django 1.8. I am using django.views.generic.View class in one of my View.

class CommonView(View):
    func_name = 'view_func'
    http_method_names = ["get", "post"]
    def get(self, request, *args, **kwargs):
        return render(request, 'template.html', {})

in urls.py i added

url(r'^common/$',CommonView),

I have written a middleware where i am doing some work with view response and overridden process_view method

class CommonMiddleware(object):
    def process_view(self, request, view_func, view_args, view_kwargs):
        response = view_func(request, *view_args, **view_kwargs)
        """ Do something"""
        return response

but in this line response = view_func(request, *view_args, **view_kwargs) I am getting error

__init__() takes exactly 1 argument (3 given)
Request Method: POST
Request URL:    http://127.0.0.1:8000/common/
Django Version: 1.8
Exception Type: TypeError
Exception Value:    
__init__() takes exactly 1 argument (3 given)

and before it I have to check the func_name of the view function. But in the middle ware class when i am trying to get the func_name attribute from view_func at that time I am getting an AttributeError that CommonView has no attribute func_name whereas I am getting it from all the function based views.

Upvotes: 1

Views: 503

Answers (1)

Sarfraz Ahmad
Sarfraz Ahmad

Reputation: 1439

The mistake is in this line

url(r'^common/$',CommonView),

you have to use

url(r'^common/$',CommonView.as_view()),

you forgot to add as_view() with your class based view while calling it from urls

Upvotes: 2

Related Questions