TIM*_*MEX 62 python authentication django http
def index(request):
the_user = request.user
Run Code Online (Sandbox Code Playgroud)
在Django中,我如何知道它是否是真正的用户?我试过了:
if the_user: 但是"AnonymousUser"即使没有人登录也是如此.所以,它总是返回true,这不起作用.
Dan*_*olo 104
你可以检查是否request.user.is_anonymous返回True.
小智 14
替代品
if user.is_anonymous():
# user is anon user
Run Code Online (Sandbox Code Playgroud)
通过测试来查看用户对象的id是什么:
if user.id == None:
# user is anon user
else:
# user is a real user
Run Code Online (Sandbox Code Playgroud)
请参阅https://docs.djangoproject.com/en/dev/ref/contrib/auth/#anonymous-users
Jef*_*wen 10
您应该检查 的值request.user.is_authenticated。它将返回True一个User实例和False一个AnonymousUser实例。
一个答案建议使用is_anonymous,但django.contrib.auth 文档说 \xe2\x80\x9cyou 应该更喜欢is_authenticated使用is_anonymous xe2\x80\x9d。
我遇到了类似的问题,只不过这是在 login_redirect_url 发送到的页面上。我必须输入模板:
{% if user.is_authenticated %}
Welcome Back, {{ username }}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32013 次 |
| 最近记录: |