Home  >  Q&A  >  body text

python - django 里自定义的 login 方法,如何使用 login_required()

根据 官方文档 写了个自定义的 login 方法如下:

from django.contrib.auth import authenticate, login

def my_login(request):
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None:
        login(request, user)
        # Redirect to a success page.
        ...
    else:
        # Return an 'invalid login' error message.
        ...

那么,在如何配合 @login_required 装饰器通过我自定义的 my_login() 方法进行登录呢?

PHP中文网PHP中文网2741 days ago917

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:30:38

    Add @login_required to other methods. If request.user is not found, jump to the login page
    Just call my_login in your login post method

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:30:38

    http://python.usyiyi.cn/djang...

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-18 10:30:38

    The decorator is used to place in the header of other view functions to truncate the request to determine whether the current request is logged in. If it is not added, there will be no such judgment.
    The login view function you defined is a view function used to log in the user. In this function, the user's attributes are bound to the request object as the user login flag.

    Purely typing on mobile phone, hope it helps!

    reply
    0
  • Cancelreply