summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Seymour <matt@mattseymour.net>2015-03-22 10:47:18 +0000
committerTim Graham <timograham@gmail.com>2015-03-24 10:30:30 -0400
commit09933aef684e4d53042eceab2e75646fe7760154 (patch)
treee24d4cdb2992be1eb99a7e95a8e0e432067b5f74
parent8f5e8ab666bcc6e2dae9f147ac804f098fcb3f78 (diff)
[1.8.x] Fixed #24501 -- Improved auth.decorators.user_passes_test() example.
Backport of fca14cd3f27a01f7ba1fe32ea9587fa75b85713a from master
-rw-r--r--docs/topics/auth/default.txt13
1 files changed, 9 insertions, 4 deletions
diff --git a/docs/topics/auth/default.txt b/docs/topics/auth/default.txt
index faad4d8579..63dcf83973 100644
--- a/docs/topics/auth/default.txt
+++ b/docs/topics/auth/default.txt
@@ -404,11 +404,12 @@ The simple, raw way to limit access to pages is to check
<django.contrib.auth.models.User.is_authenticated()>` and either redirect to a
login page::
+ from django.conf import settings
from django.shortcuts import redirect
def my_view(request):
if not request.user.is_authenticated():
- return redirect('/login/?next=%s' % request.path)
+ return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
# ...
...or display an error message::
@@ -497,16 +498,20 @@ essentially the same thing as described in the previous section.
The simple way is to run your test on :attr:`request.user
<django.http.HttpRequest.user>` in the view directly. For example, this view
-checks to make sure the user has an email in the desired domain::
+checks to make sure the user has an email in the desired domain and if not,
+redirects to the login page::
+
+ from django.shortcuts import redirect
def my_view(request):
if not request.user.email.endswith('@example.com'):
- return HttpResponse("You can't vote in this poll.")
+ return redirect('/login/?next=%s' % request.path)
# ...
.. function:: user_passes_test(func, [login_url=None, redirect_field_name=REDIRECT_FIELD_NAME])
- As a shortcut, you can use the convenient ``user_passes_test`` decorator::
+ As a shortcut, you can use the convenient ``user_passes_test`` decorator
+ which performs a redirect when the callable returns ``False``::
from django.contrib.auth.decorators import user_passes_test