简体   繁体   English

检查谁登录了 Django 管理面板

[英]Check who is logged in Django admin panel

how to check whether the user is logged in or not from admin panel如何从管理面板检查用户是否登录

what are the changes i need to make in models.py and admin.py to achieve this我需要在 models.py 和 admin.py 中进行哪些更改才能实现这一目标

from django.contrib.auth.models import User

You can't really.你真的不能。 Django Contrib Admin doesn't track this information. Django Contrib Admin 不会跟踪此信息。 The most you can get is if User has is_staff or is_superuser to check if they could potentially visit django.contrib.admin interface.你能得到的最多是用户是否有is_staffis_superuser来检查他们是否有可能访问django.contrib.admin界面。

im assuming you have two functions in your views.py我假设你的views.py中有两个函数

  1. loginUser登录用户
  2. logoutUser注销用户

create a model with user as foreignkey, for storing logged users eg.使用用户作为外键创建 model,用于存储登录用户,例如。 class loggedUsersModel class 登录用户型号

now in loginUser function现在登录用户 function

loggedUsersModel(user = request.user).save()

and in logoutUser function并在注销用户 function

loggedUsersModel(user = request.user).delete()

this will save loggedUsers这将保存loggedUsers

and register loggedUsersModel in admin.py using并使用在 admin.py 中注册 loggedUsersModel

admin.site.register(loggedUsersModel)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM