1. 程式人生 > 程式設計 >Django Auth使用者認證元件實現程式碼

Django Auth使用者認證元件實現程式碼

使用者認證元件:

  功能:用session記錄登入驗證狀態

  前提:使用者表:django自帶的auth-user

python3 manage.py createsuperuser #建立超級使用者

補充匿名使用者:

API:
  from django.contrib import auth :
    1. #if 驗證成功返回user物件,否則返回None
    user = auth.authenticate(username=user,password=pwd)
    2. auth.login(request,user) #request.user 當前登入物件
    3. auth.login(request)
    from django.contrib.auth.models import User #User == auth_user
    4. request.user.is_authenticated
    5.user = User.objects.create_user(username='',password='',email='')

  補充:
    匿名使用者物件:
      匿名使用者
      class models.AnonymousUser

      django.contrib.auth.models.AnonymousUser #這個類實現了django.contrib.auth.models.User
      藉口,但是又幾點不同:
      id永遠是None
      username永遠為空字串
      get_username()永遠返回空字串
      is_staff和is_superuser永遠是False
      is_active永遠是False
      groups和user_permissions永遠為空
      is_annonymous()返回True 而不是False
      is_authenticated()返回時False,而不是True
      set_password()、check_password()、save()和delete()引發NotImplementedError。
      New in Django 1.8:
      新增 AnonymouseUser.get_username()以更好的模擬django.contrib.auth.moudels.User總結:  if not :auth.login(request,user)  request.user = AnonymousUser()  else:request.user==登入物件  request.user是一個全域性變數

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。