Django Write class Way Verify Login

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.