Django自定義認證方式用法示例
本文實例講述了Django自定義認證方式。分享給大家供大家參考,具體如下:
創(chuàng)建登錄應用
首先創(chuàng)建一個新的login app,用來存放認證用到代碼
python manage.py startapp login
修改settings.py中的認證項
AUTHENTICATION_BACKENDS = ( 'login.auth.UsernamePasswordAuth', )
自定義認證類
在login app下創(chuàng)建auth.py文件,內容如下
#coding:utf-8 from django.contrib.auth.models import User class UsernamePasswordAuth(object): def authenticate(self, username=None, password=None): print("UsernamePasswordAuth.authenticate") try: user = User.objects.get(username__iexact=username) if user.check_password(password): return user except User.DoesNotExist: return None def get_user(self, user_id): print("UsernamePasswordAuth.get_user") try: user = User.objects.get(pk=user_id) return user except User.DoesNotExist: return None
更多關于Python相關內容感興趣的讀者可查看本站專題:《Python Socket編程技巧總結》、《Python URL操作技巧總結》、《Python數(shù)據(jù)結構與算法教程》、《Python函數(shù)使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關文章
用 Django 開發(fā)一個 Python Web API的方法步驟
這篇文章主要介紹了用 Django 開發(fā)一個 Python Web API的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12python將unicode和str互相轉化的實現(xiàn)
這篇文章主要介紹了python將unicode和str互相轉化的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05在Ubuntu 20.04中安裝Pycharm 2020.1的圖文教程
這篇文章主要介紹了在Ubuntu 20.04中安裝Pycharm 2020.1的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04Python descriptor(描述符)的實現(xiàn)
這篇文章主要介紹了Python descriptor(描述符)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11