Electronics & Programming


Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Migrating to class-based views and django.core.urlresolvers.reverse [RSS Feed]

#1 Dec. 7, 2010 20:17:52

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Migrating to class-based views and django.core.urlresolvers.reverse

Moved from django-developers:

On 7 December 2010 20:15, Sean Brant <brant.s...@gmail.com> wrote:
> Again this topic is now in django-user land.
> I do this in views.py if want the decorator on all methods (get|post).
> myview = login_required(MyView.as_view())
> On Dec 7, 2010, at 1:10 PM, Daniel Swarbrick <daniel.swarbr...@gmail.com>
> wrote:
>> That is indeed in the docs, and I have seen that. What eludes me is
>> how to use decorators more complex than login_required() from within
>> urls.py.
>> For example, this works fine:
>> from django.contrib.auth.decorators import user_passes_test
>> from django.utils.decorators import method_decorator
>> from django.views.generic import TemplateView, View
>> class IndexView(TemplateView):
>>    template_name = 'index.html'
>>   �...@method_decorator(user_passes_test(lambda u: u.is_superuser))
>>    def dispatch(self, *args, **kwargs):
>>        return super(IndexView, self).dispatch(*args, **kwargs)
>> But how would one avoid having to override the dispatch() method on
>> many classes, and put the user_passes_test() decorator in the urls.py
>> definition? Or for that matter, the permission_required() decorator?

One option is to make a base class that has the right method decorated
and then subclass it. This is what the docs show. Another is to
decorate the function returned by as_view().

>> As a side note, could a mixin be used to setup permission_required,
>> login_required etc, and user-defined class-based views be derived from
>> multiple parent classes?

Yes. Some decorators make sense as Mixins, others don't - it's a
matter of judgement. IMHO, login_required doesn't make much sense as a

>> Sorry if this has meandered into django-users land... maybe some
>> advanced CBV examples in the docs?

Here's a view from my project:

class FileCollectionView(JSONMixin, PersonMixin, SingleObjectMixin, View):
# lots of secret code :)

This uses view_decorator() function I wrote, which is a shortcut
for overriding ``as_view`` method (I prefer to override that instead
of ``dispatch``). Maybe you'll find it helpful.

Łukasz Rekucki

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


  • Root
  • » Django
  • » Migrating to class-based views and django.core.urlresolvers.reverse [RSS Feed]

Board footer

Moderator control

Enjoy the 27th of May

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support