In a view function, some operations require a user to be logged on.
Method One:
Use the request method to get the user login information:
If request.user.is_authenticated ():
.. Else: Return
render (Requeset, ' login.html ')
1 2 3 4
Method Two:
You can determine whether a user is logged on by inheriting an underlying class in an attempt function.
Define a base class in utils.py:
From django.contrib.auth.decorators import login_required from
django.utils.decorators import method_decorator
class Loginrequiredmixin (object): ""
determines whether the user is logged on to the
@method_decorator (login_required login_ Url= '/login/')
def dispatch (self, request, *args, **kwargs): Return
super (loginrequiredmixin, self). Dispatch (Request, *args, **kwargs)
1 2 3 4 5 6 7 8 9 10 11
When you write a view function, you inherit the class:
From utils import Loginrequiredmixin
class Coursecommentview (Loginrequiredmixin, View): "" "
Course Review "" "
def get (self, request):
Pass
1 2 3 4 5 6 7 8
The second way to facilitate the inheritance of many view functions, the first way for the content of the page to determine the part.