'function' object has no attribute 'as_view'

7,286 views
Skip to first unread message

youpsla

unread,
Nov 23, 2011, 6:03:03 AM11/23/11
to Django users
Hello,
the issue here seems to be trivial, maybe I got something in my eyes,
but I can't find the solution.

Here is my code:

urls.py
url(r"magasin/(?P<magasin_id>\d+)/evenement/new/$",
EvenementCreateView.as_view(model=Evenement),
name='new_evenement_magasin'),

views.py:
class EvenementCreateView(CreateView):
form_class = EvenementForm
sucess_url = 'user_magasins'

def dispatch(self, *args, **kwargs):
bla bla bla ....

def form_valid(self, form):
bl bla bla .....
return HttpResponseRedirect(self.get_success_url())


What is funny here is whatever url of the site I call, even one which
doesn't use this view I've the error describe in the subject. The
traceback point on the line of the urls.py.

I'm not python and django proof, but as far I know class blablabla()
define a Class and not a function !!!

Does someone have an idea here ?

Regards

Alain

Ivo Brodien

unread,
Nov 23, 2011, 6:31:22 AM11/23/11
to django...@googlegroups.com
does this work?

...
(r"magasin/(?P<magasin_id>\d+)/evenement/new/$",EvenementCreateView.as_view(model=Evenement,)),
...


I haven’t used Class based Views yet, but everytime they show up in the docs it is always without using the url function and giving a name. So this is just a wild guess.

It seems like it is interpreting EvenementCreateView as a view function not as a class and does not let you call as_view.

The error happens always because django looks at all the urls you configured in order to know which one to use, this is why it always crashes.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

youpsla

unread,
Nov 23, 2011, 6:47:13 AM11/23/11
to django...@googlegroups.com
Hi,
thnaks for your answer. It's the same. it doesn't work.

For information I use generic view for other part of the site with:
url(r"magasin/(?P<pk>\d+)/supprimer/$", DeleteView.as_view(model=Magasin, success_url="/magasins/liste"), name='magasin_supprimer'),

and it works fine.

And has you say, django seems to believe that EvenementCreateView from views.py is a function, but the declaration is:
class EvenementCreateView(CreateView):
.........

That's what I dont understand.

I use the same schema for other part and it works fine. I've read my code caracter after caracter to compare .... it looks very very similar ....


Still a mystery for me. 

Again, thanks for your answer


Alain

Ivo Brodien

unread,
Nov 23, 2011, 7:36:36 AM11/23/11
to django...@googlegroups.com
yeah, very strange.


url(r"magasin/(?P<magasin_id>\d+)/evenement/new/$",
EvenementCreateView.as_view(model=Evenement),
name='new_evenement_magasin'),

and you are sure the error is caused by the line above? Commenting it out and works? Just asking, since you did not provide the whole error description?

youpsla

unread,
Nov 23, 2011, 9:06:59 AM11/23/11
to django...@googlegroups.com
Hi again,
yes I'm sure the error comes from here. Bellow is the full trace back:

Environment:


Request Method: GET

Django Version: 1.3.1
Python Version: 2.7.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'south',
 'clients',
 'django.contrib.admin',
 'categories',
 'magasins',
 'registration',
 'profiles',
 'evenements',
 'commandes',
 'debug_toolbar']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "C:\Python27\Lib\site-packages\django\core\handlers\base.py" in get_response
  89.                     response = middleware_method(request)
File "C:\Python27\Lib\site-packages\debug_toolbar\middleware.py" in process_request
  70.                 original_urlconf = __import__(getattr(request, 'urlconf', settings.ROOT_URLCONF), {}, {}, ['*'])
File "C:\dev\Flash\urls.py" in <module>
  25.     (r'magasin/(?P<magasin_id>\d+)/evenement/new/$', EvenementCreateView.as_view(model=Evenement)),

Exception Type: AttributeError at /magasin/
Exception Value: 'function' object has no attribute 'as_view'


Any ideas are welcome, because this error let me dry !!! huuuu


Regards

Alain

eprikazc

unread,
Nov 23, 2011, 7:23:40 AM11/23/11
to Django users
You might be overriding EvenementCreateView somewhere else. To check
it I would suggest you to look up all occurencies of
EvenementCreateView in your files

Eugene

eprikazc

unread,
Nov 23, 2011, 7:15:21 AM11/23/11
to Django users
You might be re-defining EvenementCreateView somewhere else as a
function. To check it, I would suggest you to look up all occurences
of "EvenementCreateView" in your files.

youpsla

unread,
Nov 23, 2011, 11:22:00 AM11/23/11
to django...@googlegroups.com
Hello all,
I've verified that I don't override the class.

I've found the reason of the bug. When I delete "@login_required", it works.

My class was in view.py:

@login_required
class EvenementCreateView(.....):
.....


I apologize for not have copy/paste that in my first post.

I've done the test with another class, it's the same. Then I think @login_required works only for function and not for Class.

I don't know what happend internally ... But for now it's ok for me. I'm building the POC. But for production purpose, it will be an issue.



Thanks all for your time.

Regards

Alain

Ivo Brodien

unread,
Nov 23, 2011, 11:46:42 AM11/23/11
to django...@googlegroups.com

I've done the test with another class, it's the same. Then I think @login_required works only for function and not for Class.

youpsla

unread,
Nov 23, 2011, 12:05:15 PM11/23/11
to django...@googlegroups.com
Thanks for all.

Agai I apologize for not having post this "etail" in my originl post.

Regards

Alain
Reply all
Reply to author
Forward
0 new messages