summaryrefslogtreecommitdiff
path: root/docs/topics/class-based-views
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2014-03-17 06:49:59 -0400
committerTim Graham <timograham@gmail.com>2014-03-17 06:49:59 -0400
commit584044566414d3f4595337225aa23b52a089a680 (patch)
treee8bde37c7a222d98de6637f10f2023c41ea412cb /docs/topics/class-based-views
parentad43fdaa0008ce8d3d05e840f67af631d1329489 (diff)
Fixed #22006 -- Documented how to write a login_required mixin for CBVs.
Thanks django at patjack.co.uk for the suggestion and mockforest for the draft patch.
Diffstat (limited to 'docs/topics/class-based-views')
-rw-r--r--docs/topics/class-based-views/intro.txt23
1 files changed, 23 insertions, 0 deletions
diff --git a/docs/topics/class-based-views/intro.txt b/docs/topics/class-based-views/intro.txt
index 906205b96c..c307fc603d 100644
--- a/docs/topics/class-based-views/intro.txt
+++ b/docs/topics/class-based-views/intro.txt
@@ -173,6 +173,29 @@ that inherits from ``View`` - for example, trying to use a form at the top of a
list and combining :class:`~django.views.generic.edit.ProcessFormView` and
:class:`~django.views.generic.list.ListView` - won't work as expected.
+.. _mixins_that_wrap_as_view:
+
+Mixins that wrap ``as_view()``
+------------------------------
+
+One way to apply common behavior to many classes is to write a mixin that wraps
+the :meth:`~django.views.generic.base.View.as_view()` method.
+
+For example, if you have many generic views that should be decorated with
+:func:`~django.contrib.auth.decorators.login_required` you could implement a
+mixin like this::
+
+ from django.contrib.auth.decorators import login_required
+
+ class LoginRequiredMixin(object):
+ @classmethod
+ def as_view(cls):
+ return login_required(super(LoginRequiredMixin, cls).as_view())
+
+ class MyView(LoginRequiredMixin, ...):
+ # this is a generic view
+ ...
+
Handling forms with class-based views
=====================================