There are many reasons to control a user's access to a part of the site.
A simple and primitive restriction method is to check request.user.is_authenticated () and redirect to the landing page:
From django.http import httpresponseredirectdef my_view (Request): if not request.user.is_authenticated (): Return Httpresponseredirect ('/accounts/login/?next=%s '% request.path) # ...
or display an error message:
def my_view (Request): if not request.user.is_authenticated (): return Render_to_response (' Myapp/login_ Error.html ') # ...
As a shortcut, you can use the handy login_required modifier:
From django.contrib.auth.decorators import login_required@login_requireddef my_view (Request): # ...
Login_required do the following things:
If the user is not logged in, redirect to/accounts/login/, and pass the current absolute URL as next in the query string past, for example:/accounts/login/?next=/polls/3/.
If the user is already logged in, perform the view function normally. The view code can assume that the user is already logged in.
=