根据 官方文档 写了个自定义的 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()
方法进行登录呢?
大家讲道理2017-04-18 10:30:38
裝飾器是用來放在其他view函數頭,來截斷請求判斷目前請求的是否是登入的,若不加便不會有這個判斷。
而你定義的登入view函數,是一個用來使用戶登入的view函數,在這個函數裡把使用者的屬性綁定在了request 這個物件上,作為使用者登入標誌。
純手機打字,望有幫助!