在某些情况下,我想让工作人员踢出一些用户。 我的django 1.8网站以redis方式存储会话。
我在视图中尝试了this solution:
#will be removed in 1.9
from django.utils.importlib import import_module
@staff_member_required
def kickout_user(request, username):
u = User.objects.get(username = username)
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
active_users = Request.objects.active_users(seconds=60)
active_users_ids = [user.id for user in active_users]
for session in stored_sessions:
SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
s = SessionStore(session_key=session.session_key)
session_uid = session.get_decoded().get('_auth_user_id')
print 'session', session_uid
if session_uid == u.id:
print 'session going to be deleted for uid:', session_uid
session.delete()
print ' session deleted'+ u.username
但它给出了这个错误:
global name 'Request' is not defined
还有一些建议here,但它们要么是瑕疵,要么是基于编写额外的中间件,我觉得这些中间件有些过分,并试图进行aovid。