[英]How to enable non-superusers login from front end in Django?
我為登錄創建了一個視圖,但 Django 僅對超級用戶進行身份驗證。 非超級用戶無法登錄。我只希望非超級用戶從前端登錄
def logingin(request):
if request.method == 'POST':
username = request.POST.get('username','')
password = request.POST.get('password','')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('/')
else:
return redirect('/signup')
return render(request, 'login.html')
誰能告訴我我需要在我的代碼中做哪些修改?
您的非超級用戶的is_active標志必須設置為 false
https://docs.djangoproject.com/en/2.2/ref/contrib/auth/#django.contrib.auth.models.User.is_active
下面是摘錄
這不一定控制用戶是否可以登錄。身份驗證后端不需要檢查 is_active 標志,但默認后端 (ModelBackend) 和 RemoteUserBackend 可以。 如果要允許非活動用戶登錄,可以使用 AllowAllUsersModelBackend 或 AllowAllUsersRemoteUserBackend。 在這種情況下,您還需要自定義 LoginView 使用的 AuthenticationForm,因為它拒絕非活動用戶。 請注意,諸如 has_perm() 之類的權限檢查方法和 Django 管理員中的身份驗證對於非活動用戶都返回 False。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.