1460 | | To limit access to a :doc:`generic view </ref/generic-views>`, write a thin |
1461 | | wrapper around the view, and point your URLconf to your wrapper instead of the |
1462 | | generic view itself. For example:: |
| 1460 | Controlling access to a :doc:`class-based generic view </ref/class-based-views>` |
| 1461 | is done by decorating the :meth:`View.dispatch <django.views.generic.base.View.dispatch>` |
| 1462 | method on the class. For example:: |
| 1463 | |
| 1464 | from django.contrib.auth.decorators import login_required |
| 1465 | from django.utils.decorators import method_decorator |
| 1466 | from django.views.generic import TemplateView |
| 1467 | |
| 1468 | class ProtectedView(TemplateView): |
| 1469 | template_name = 'secret.html' |
| 1470 | |
| 1471 | @method_decorator(login_required) |
| 1472 | def dispatch(self, *args, **kwargs): |
| 1473 | return super(ProtectedView, self).dispatch(*args, **kwargs) |
| 1474 | |
| 1475 | See :ref:`decorating-class-based-views` for the details. |
| 1476 | |
| 1477 | Function-based generic views |
| 1478 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 1479 | |
| 1480 | To limit access to a :doc:`function-based generic view </ref/generic-views>`, |
| 1481 | write a thin wrapper around the view, and point your URLconf to your wrapper |
| 1482 | instead of the generic view itself. For example:: |