【问题标题】:django logout user based on usernamedjango 根据用户名注销用户
【发布时间】:2023-04-06 03:03:01
【问题描述】:

我正在尝试强制注销不再活跃的 django 用户。我需要能够通过用户名强制用户注销。在做了一些研究之后,我发现 django 可以选择使用 logout(request) 注销,但它没有给我选择基于用户名的注销选项。 有人有任何想法吗? 谢谢

我已尝试针对我的问题测试以下代码,该代码取自How to force user logout in django? 在我的模型中:

from django.contrib.auth.models import User
from django.db import models
from datetime import datetime

class MyUser(User):
force_logout_date = models.DateTimeField(null=True, blank=True)

def force_logout(self):
    self.force_logout_date = datetime.now()
    self.save()

然后我在views.py中添加了这部分:

from myapp.models import MyUser
MyUser.objects.get(username='johndoe').force_logout()

但我现在有以下错误:

MyUser matching query does not exist.

Request Method:     POST
Request URL:    http://localhost:8000/datatableuser/?    
typeoftask=edit&username=xyzstaff
Django Version:     1.8.11
Exception Type:     DoesNotExist
Exception Value:    

MyUser matching query does not exist.

Exception Location:     /usr/local/lib/python2.7/dist-  
packages/django/db/models/query.py in get, line 334
Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    

['/home/paul/Desktop/djangoproject/peptidetrackerdatabase/src',
'/usr/local/lib/python2.7/dist-packages/setuptools-20.3.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/pip-8.1.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/Sphinx-1.4-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/imagesize-0.7.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/alabaster-0.7.7-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/Babel-2.2.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/snowballstemmer-1.2.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/docutils-0.12-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/Pygments-2.1.3-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/Jinja2-2.8-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/MarkupSafe-0.23-py2.7-linux-x86_64.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-x86_64-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PILcompat',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

Server time:    Thu, 14 Apr 2016 22:12:42 -0700    enter code here

【问题讨论】:

    标签: python django django-admin django-authentication django-login


    【解决方案1】:

    这主要取决于您如何进行身份验证。不知道确切的答案是不可能给你答案的。

    但是,假设您使用的是默认的 Django 身份验证,这个答案应该仍然有效:

    How to force user logout in django?

    【讨论】:

    • 您好,感谢您的回复。我看到了这个解决方案,但还不知道如何实现它,因为我的 Session.objects.all() 总是返回空列表,我不知道为什么
    • @Paul85 请检查“django.contrib.sessions”是否在您安装的应用程序中,“django.contrib.sessions.middleware.SessionMiddleware”是否在您激活的中间件中,用户可以登录并且迁移是应用。另外,请在您的数据库中手动验证会话表是否存在且不为空。
    • 我已经检查过了,我的 settings.py 没问题。
    猜你喜欢
    • 2021-11-23
    • 2019-01-05
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 2023-03-27
    • 2018-08-07
    相关资源
    最近更新 更多