惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
Webroot Blog
Webroot Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Threat Research - Cisco Blogs
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
Recorded Future
Recorded Future
S
Schneier on Security
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The GitHub Blog
The GitHub Blog
S
Security @ Cisco Blogs
O
OpenAI News
W
WeLiveSecurity
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
Simon Willison's Weblog
Simon Willison's Weblog
人人都是产品经理
人人都是产品经理
Cloudbric
Cloudbric
The Last Watchdog
The Last Watchdog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
NISL@THU
NISL@THU
T
Tailwind CSS Blog
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
D
DataBreaches.Net
B
Blog RSS Feed
N
News and Events Feed by Topic
N
News and Events Feed by Topic
H
Heimdal Security Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
Latest news
Latest news
V
Vulnerabilities – Threatpost
Hacker News: Ask HN
Hacker News: Ask HN
WordPress大学
WordPress大学
V
V2EX
aimingoo的专栏
aimingoo的专栏
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
Help Net Security
Help Net Security

博客园 - hgdfr

Django1.5内置的用户认证系统介绍(之五)在admin后台管理用户 --by hillfree Django1.5内置的用户认证系统介绍(之四)Authentication in Web requests --by hillfree Django1.5内置的用户认证系统介绍(之三)权限与授权--by hillfree Django1.5内置的用户认证系统介绍(之一)--by hillfree Django REST Framework Tutorial 5:关系与超链接API(中文版教程)by hillfree Django REST Framework Tutorial 4:认证与权限(中文版教程)by hillfree Django REST Framework Tutorial 3:基于类的Views(中文版教程)by hillfree Django1.5+Python3.3下groundwork的使用 Django1.5中设置静态目录的简要说明 参照《鲜活的数据:数据可视化指南》第2章:抓取网页数据(历史天气记录)的Python程序 Python3.3中如何产生伪随机数 《鲜活的数据:数据可视化指南》第2章 收集数据 Python3.3源码 CSCW领域的“老”词和“新”词 FOAF简介和朋友圈子 Class Library类型的工程难道不能用app.config? 配置文件中的DataDirectory在那里设置? 自己写的类需要重写ToString(), HashCode(), Equal()吗? 注释中如果出现尖括号怎么办?“<” - hgdfr - 博客园 Access Control Issues 有关访问控制
Django1.5内置的用户认证系统介绍(之二)使用User对象--by hillfree
hgdfr · 2013-04-13 · via 博客园 - hgdfr

说明:网上有关Django用户系统的内容不少,但是好像没有针对Django1.5的。可能是因为Django1.5目前用的人还不多吧。(python3.x貌似也是这种情况)。因为自己要用,所以顺手在学习官方文档的过程中,边看边译。基本忠于原文(>95%),小部分太啰嗦的内容就适当意译、简化了。后续还会加入部分自己的体会(会注出),供参考。如有错讹、不准确之处,还请大家指教。

Source:https://docs.djangoproject.com/en/1.5/topics/auth/default/

使用Django认证系统(authentication system)

本文介绍Django UAS在缺省配置下的基本功能用法。它几乎能够满足大部分的项目相关需求。对那些基本功能无法满足的项目,Django还支持认证与授权功能的扩展和定制化(extension and customization

Again,Django UAS包括了认证和授权,因为这两部分的功能相互关联,所以只用UAS/认证系统(authentication system)来指示这两部分。

用户对象 User objects

用户对象是认证系统的核心。用户对象通常用来代表网站的用户,并支持例如访问控制、注册用户、关联创建者和内容等。在Django认证框架中只有一个用户类,例如超级用户( ‘superusers’)或职员(‘staff’)用户只不过是相同用户对象设置了不同属性而已。

缺省用户的基本属性包括:

  • 用户名username
  • 密码password
  • email
  • 名字first name
  • 姓氏last name

参见完整的API文档(full API documentation),本文档的下列部分是面向任务方式编排的。

创建用户 Creating users

创建用户最直接的方法是使用内置的 create_user() 方法:

>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword')
 
# At this point, user is a User object that has already been saved
# to the database. You can continue to change its attributes
# if you want to change other fields.
>>> user.last_name = 'Lennon'
>>> user.save()

如果你设置好了Django admin后台,你还可以使用Web后台来创建用户(create users interactively)。

创建超级用户 Creating superusers

如果你已经在配置文件的 INSTALLED_APPS里配置了'django.contrib.auth' ,那么在你第一次运行manage.py syncdb 时,会提示你创建一个超级用户。如果你在之后还想创建超级用户,你可以使用如下的命令行:

manage.py createsuperuser --username=joe --email=joe@example.com

程序会提醒你设置一个密码,如果输入了密码,用户就会被立即创建。如果你没有使用 --username 或 --email 选项,那么还会提示你输入这些内容。

更改密码 Changing passwords

Django不在用户模型(数据库表)中保存密码文本 (clear text),而只保存哈希值 (具体参见documentation of how passwords are managed)。因此,不要去直接操作用户的password属性。这也是为什么要提供helper函数来创建用户的原因。

可以由多个方法来更改用户密码:

manage.py changepassword *username* 提供了一个更改密码的命令行方式。它会提示输入新密码并确认输入,如果两次输入一致,新密码会立即生效。如果你未指定用户,程序会尝试指定修改当前系统用户的密码。

你也可以用程序来更改密码,方法是 set_password()

>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username__exact='john')
>>> u.set_password('new password')
>>> u.save()

如果你配置好了Django admin后台,你也可以使用Web后台来修改用户密码(authentication system’s admin pages

Django提供了相关的 views 和 forms 可供用户使用修改密码。

用户认证Authenticating Users

authenticate(**credentials)

需要验证username 和password时,使用 authenticate()方法。它接受表单参数提供的安全凭证(credentials)做参数,在缺省配置下,是 username 和password,如果验证通过就返回一个User对象,如果密码无效,则返回None:

from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:
    # the password verified for the user
    if user.is_active:
        print("User is valid, active and authenticated")
    else:
        print("The password is valid, but the account has been disabled!")
else:
    # the authentication system was unable to verify the username and password
    print("The username and password were incorrect.")