django user authentication user objects

Internal users in django model with a lot of property methods, we can directly use

1 is_staff

Boolean. Determine whether the user can access the admin interface. Default False.

2 is_active

Boolean. Whether active user, default True. Generally do not delete the user, but the user's is_active set to False.

3 is_authenticated()

Whether the user is authenticated, landing.

4 make_password(password)

Django comes to encryption password encryption function is hash with salt

5 check_password(password)

Check the password entered by the user is correct

6 set_password(password)

Change password

7 authenticate()

 

from django.contrib.auth import authenticate, login, logout
def post(self, request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    form = LoginForm(request.POST)
    if form.is_valid():
        try:
            user = UserProfile.objects.get(Q(username=username)|Q(email=username)|Q(mobile=username))
        except:
            return render(request, 'login.html', {'errors': u"用户不存在!请先注册"})
        if user.is_active:
            user1 = authenticate(username=username, password=password)
            if user1 is None:
                return render(request, 'login.html', {'errors': u"用户名/密码有误!"})
            login(request, user1)
            the render return (Request, 'index.html')
        else:
            return render (request, 'login.html' , { 'errors': u " that the user has not been activated!"}) 
    return the render (Request, 'the login.html', { 'form': form})
8 last_login

 

The last login time, as a datetime object, the default for the time of day.

 

user.last_login  =  timezone.now()
9 request.user.is_authenticated()

 

Can determine whether the current user login

10 request.user.username

Get the current logged in user's username

 

 

Guess you like

Origin www.cnblogs.com/ellisonzhang/p/11445162.html