简体   繁体   English

未登录时提供“默认”用户而不是匿名用户

[英]Providing 'default' User when not logged in instead of AnonymousUser

Is there a way to globally provide a custom instance of User class instead of AnonymousUser ?有没有办法全局提供User类的自定义实例而不是AnonymousUser

It is not possible to assign AnonymousUser instances when User is expected (for example in forms, there is need to check for authentication and so on), and therefore having an ordinary User class with name 'anonymous' (so that we could search for it in the DB) would be globally returned when a non-authenticated user visits the page.不可能在需要User时分配AnonymousUser实例(例如在表单中,需要检查身份验证等),因此有一个名为“anonymous”的普通User类(以便我们可以搜索它在数据库中)将在未经身份验证的用户访问页面时全局返回。 Somehow implementing a custom authentication mechanism would do the trick?以某种方式实现自定义身份验证机制可以解决问题吗? And I also want to ask if such an idea is a standard approach before diving into this.在深入研究之前,我还想问问这样的想法是否是一种标准方法。

您可以创建一个自定义中间件(在 AuthenticationMiddleware 之后调用),它检查用户是否登录,如果没有,则用您选择的用户替换附加到请求的当前用户对象。

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

相关问题 Tastypie将已登录用户识别为AnonymousUser - Tastypie recognizes logged in user as AnonymousUser 在Django中使用all-auth接收到user_logged_in信号后,将用户设置为AnonymousUser - User is set to AnonymousUser after a user_logged_in signal is received in Django with all-auth 获取用户在Django上返回“ AnonymousUser” - get user returns “AnonymousUser” on django 如何将当前登录的用户默认传递给表单 - How to pass the currently logged in user as default to a form 当没有给出 url 参数时,如何在 Django 视图中将当前登录的用户设置为默认值 - How do i set the current logged in user as default in Django view when no url parameters are given 登录时未显示用户预订 - User bookings not appearing when logged in 用户在Django中的哪个位置成为AnonymousUser? - Where in Django does a user become an AnonymousUser? AbstractUser 上的 request.user 给出 AnonymousUser TypeError - request.user on AbstractUser gives AnonymousUser TypeError 当我从JS客户端发出请求时,为什么Django User是AnonymousUser,但是在使用Django Rest Framework时正确设置了它? - Why is Django User being AnonymousUser when I make a request from a JS client but it is properly set when using Django Rest Framework ? Django 3 - Model.save() 为主键提供默认值时 - Django 3 - Model.save() when providing a default for the primary key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM