如何告诉Django用户是经过验证的?

时间:2021-01-30 07:09:34

In this Django code I inherited there is a check for request.user.is_authenticated().

在我继承的Django代码中,有一个request.user.is_authenticated()的检查。

How do I set this authenticated attribute for a user, in particular when I am doing a registration through AJAX JSON?

如何为用户设置这个经过身份验证的属性,特别是当我通过AJAX JSON进行注册时?

2 个解决方案

#1


2  

To log a user in, you should django.contrib.auth.login - see the docs here: https://docs.djangoproject.com/en/1.5/topics/auth/default/#auth-web-requests

要记录一个用户,你应该去django.auth。登录——请参阅这里的文档:https://docs.djangoproject.com/en/1.5/topics/auth/default/# autht/ # autht-web请求

Note, though, that you should authenticate the user (i.e. check their credentials) before you do so, with django.contrib.auth.authenticate - same docs as above.

不过,请注意,在使用django. managed .auth进行身份验证之前,您应该对用户进行身份验证(即检查他们的凭证)。认证-和上面一样的文档。

This is regardless of whether you're using AJAX or not - this code has to be in a view somewhere that gets called in order for the user to get logged in. Whether that view is called via AJAX or not is irrelevant.

这与您是否使用AJAX无关——该代码必须位于某个视图中,以便用户登录。该视图是否通过AJAX调用无关紧要。

#2


0  

The only user this will return false for is AnonymousUser; all other users have it return true via their superclass. Therefore all you need to do is authenticate the user normally.

这个返回false的唯一用户是匿名用户;所有其他用户都通过他们的超类返回true。因此,您需要做的就是正常地对用户进行身份验证。

#1


2  

To log a user in, you should django.contrib.auth.login - see the docs here: https://docs.djangoproject.com/en/1.5/topics/auth/default/#auth-web-requests

要记录一个用户,你应该去django.auth。登录——请参阅这里的文档:https://docs.djangoproject.com/en/1.5/topics/auth/default/# autht/ # autht-web请求

Note, though, that you should authenticate the user (i.e. check their credentials) before you do so, with django.contrib.auth.authenticate - same docs as above.

不过,请注意,在使用django. managed .auth进行身份验证之前,您应该对用户进行身份验证(即检查他们的凭证)。认证-和上面一样的文档。

This is regardless of whether you're using AJAX or not - this code has to be in a view somewhere that gets called in order for the user to get logged in. Whether that view is called via AJAX or not is irrelevant.

这与您是否使用AJAX无关——该代码必须位于某个视图中,以便用户登录。该视图是否通过AJAX调用无关紧要。

#2


0  

The only user this will return false for is AnonymousUser; all other users have it return true via their superclass. Therefore all you need to do is authenticate the user normally.

这个返回false的唯一用户是匿名用户;所有其他用户都通过他们的超类返回true。因此,您需要做的就是正常地对用户进行身份验证。