From 6da28d5edfaee1858c8f7ae603a717aaa05e15cf Mon Sep 17 00:00:00 2001 From: Daniel Roseman Date: Thu, 31 Jan 2019 23:01:53 +0000 Subject: [PATCH] Used LoginRequiredMixin in "Models and request.user" example. --- docs/topics/class-based-views/generic-editing.txt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/topics/class-based-views/generic-editing.txt b/docs/topics/class-based-views/generic-editing.txt index a89483ec5a..7124b146ac 100644 --- a/docs/topics/class-based-views/generic-editing.txt +++ b/docs/topics/class-based-views/generic-editing.txt @@ -206,10 +206,11 @@ to edit, and override .. code-block:: python :caption: views.py + from django.contrib.auth.mixins import LoginRequiredMixin from django.views.generic.edit import CreateView from myapp.models import Author - class AuthorCreate(CreateView): + class AuthorCreate(LoginRequiredMixin, CreateView): model = Author fields = ['name'] @@ -217,11 +218,9 @@ to edit, and override form.instance.created_by = self.request.user return super().form_valid(form) -Note that you'll need to :ref:`decorate this -view` using -:func:`~django.contrib.auth.decorators.login_required`, or -alternatively handle unauthorized users in the -:meth:`~django.views.generic.edit.ModelFormMixin.form_valid()`. +:class:`~django.contrib.auth.mixins.LoginRequiredMixin` prevents users who +aren't logged in from accessing the form. If you omit that, you'll need to +handle unauthorized users in :meth:`~.ModelFormMixin.form_valid()`. AJAX example ============