User authentication in Django | Python

來源:互聯網
上載者:User

標籤:superuser   使用者認證   增刪改查   建立   使用者密碼   區別   登入   lock   後台管理   

1.User使用者表預設欄位

  username,password,email,first_name,last_name  

 

2.添加User表資料

from django.contrib.auth.models import Useruser = User.objects.create_user(‘mic‘,‘[email protected]‘,‘micpassword‘)user.save()

 

3.建立超級使用者

python3 manage.py createsuperuser 

建立超級使用者,與上面添加的使用者區別在於被賦予了許可權,可以登入django內建admin後台管理資料;

 

4.修改使用者密碼

終端修改:python3 manage.py changepassword username new_password

代碼修改;

admin後台修改;

from django.contrib.auth.models import User

u = User.objects.get(username="mic")u.set_password("zxcvbn123")u.save()

 

5.使用者認證

django推薦使用LoginView視圖類來處理,而不是使用下面的例子;

>>> from django.contrib.auth.models import User>>> from django.contrib.auth import authenticate>>> user = authenticate(username="mic",password="zxcvbn123")>>> user<User: mic>>>> if user:...     print("認證成功!")...認證成功!>>> user = authenticate(username="mic",password="wrong_password")>>> user
>>>

 

6.使用者組與許可權

django內建使用者組表與許可權表,此外,還有:使用者/使用者組,使用者/許可權,使用者組/許可權 這三張額外產生的多對多表,即使用者,使用者組,許可權id的對應關係;

怎麼將許可權移植到自己做的後台管理系統中呢?

就是對這幾張表的增刪改查;

django內建的許可權是對所有表的增刪改查的賦權,如果需要設定對單個表的許可權,需要自己構造;

待補充...

 

User authentication in Django | Python

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.