User authentication in Django(用户认证)
一,概述:
auth 系统包括:
1)Users
2)Permissions: Binary (yes/no) flags designating whether a user may perform a certain task.(权限:二进制(是/否)标志,指示用户是否可以执行某个任务。)
3)Groups: A generic way of applying labels and permissions to more than one user.(组:向多个用户应用标签和权限的通用方法。)
4)A configurable password hashing system(一个可配置的密码散列系统)
5)Forms and view tools for logging in users, or restricting content(用于用户登录或者限制内容的表单和视图工具)
6)A pluggable backend system(一个可插入的后台系统)
7)Password strength checking(密码强度检查)
8)Throttling of login attempts(限制登录尝试)
9)Authentication against third-parties (OAuth, for example)(针对第三方的认证,例如(OAuth))
Installation(安装)
1)INSTALLED_APPS中配置 django.contrib.auth' 和django.contrib.contenttypes
2)在MIDDLEWARE中配置 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
Usage(用法)
一,Using the Django authentication system¶(使用Django 认证系统)
1)User objects:The primary attributes of the default user are:(默认用户的主要属性): username, password, email,firstname,lastname
2)Creating users:
>>> 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()
3)Creating superusers
$ python manage.py createsuperuser --username=joe --email=joe@example.com
4)Changing passwords
>>> from django.contrib.auth.models import User
>>> u = User.objects.get(username='john')
>>> u.set_password('new password')
>>> u.save()
5)Authenticating users
from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:
# A backend authenticated the credentials
else:
6)Authentication in Web requests判断用户是否登录
if request.user.is_authenticated:
# Do something for authenticated users.(已登录)
...
else:
# Do something for anonymous users.
7)How to log a user in(如何登录用户)
from django.contrib.auth import authenticate, login def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
...
else:
# Return an 'invalid login' error message.
8)How to log a user out¶(如何退出)
from django.contrib.auth import logout def logout_view(request):
logout(request)
# Redirect to a success page.
8)登录装饰器(The login_required decorator¶)
from django.contrib.auth.decorators import login_required @login_required
def my_view(request):
User authentication in Django(用户认证)的更多相关文章
- django用户认证系统——拓展 User 模型
Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...
- django用户认证系统——拓展 User 模型2
Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...
- Django——用户认证
Django--用户认证 用户与Authentication(身份验证) Django 用户认证系统处理用户帐号,组,权限以及基于cookie的用户会话. 这个系统一般被称为 auth/auth (认 ...
- [django]用户认证中只允许登陆用户访问(网页安全问题)
当设计一个重要网页时,一般要求未从登陆界面访问的用户不能进入其他页面,那么需要如何设置呢? 如下 django中的url.py urlpatterns = [ url(r'^$', 'login ...
- Django 用户认证及OneToOneField
Django 用户认证如果自己不想写 就可以用django自带的认证 首选导入模块 models.py #!/usr/bin/env python #_*_ coding:utf8 _*_ from ...
- “Django用户认证系统”学习资料收集
首推追梦人物——Django用户认证系统 待续……
- Django 2.0 学习(17):Django 用户认证(auth模块)
Django 用户认证(auth模块) 一.认证登陆 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中:如果用户存在于数据库中,然后再验证用户输入 ...
- django用户认证系统——重置密码7
当用户不小心忘记了密码时,网站需要提供让用户找回账户密码的功能.在示例项目中,我们将发送一封含有重置用户密码链接的邮件到用户注册时的邮箱,用户点击收到的链接就可以重置他的密码,下面是具体做法. 发送邮 ...
- django用户认证系统——修改密码6
再此之前我们已经完成了用户登录.注册.注销等功能,接下来让我们继续为用户提供修改密码的功能.该功能 Django 的 auth 应用也已经为我们提供,过程几乎和之前的登录功能完全一样. 编写修改密码模 ...
随机推荐
- 電腦清理緩存bat文件源碼
@echo off echo 正在清除系統垃圾文件,請稍等 ...... del /f /s /q %systemdrive%\*.tmp del /f /s /q %systemdrive%\*._ ...
- ansible常用模块入门
常用模块有以下几个 command copy shell crond yum service setup 1.command模块 ansible george -m command -a " ...
- 2-Add Two Numbers @LeetCode
2-Add Two Numbers @LeetCode 题目 思路 题目中得到的信息有: 这是两个非负数,每位分别保存在链表的一个结点上: 逆序保存,从低位到高位依次. 一般整数的相加都是从低往高进行 ...
- 左耳听风-ARTS-第4周(2019/4/21-2019/4/27)
Algorithm 本周的算法题是删除已排序数据中的重复数字(https://leetcode.com/problems/remove-duplicates-from-sorted-array/).这 ...
- Vue01
1.vue.js库的下载 vue.js是目前前端web开发最流行的工具库,由尤雨溪在2014年2月发布的. 另外几个常见的工具库:react.js /angular.js 官方网站: 中文:https ...
- Ignite(二): 架构及工具
1.集群和部署 Ignite集群基于无共享架构,所有的集群节点都是平等的,独立的,整个集群不存在单点故障. 通过灵活的Discovery SPI组件,Ignite节点可以自动地发现对方,因此只要需要, ...
- 工控随笔_21_西门子_WinCC的VBS脚本_10_对象_01
最近有点小忙,各种事情,心情也不是很好,烦心事特别多,因此最近更新的比较慢. 不敢再松懈了,今天正好有点时间,就继续看了一下VBScript中关于对象的一些内容. 一.对象 OOP是编程规模发展到一定 ...
- 解决Table不继承父节点的属性的方法
解决Table不继承父节点的属性的方法 发现table不继承父节点的属性. 解决方法:给html文件加上<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...
- nginx1.14.0版本location路径配置四种方式
假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问. 第一种:location /proxy/ { proxy_pass http:// 12 ...
- MySql 的SQL执行计划查看,判断是否走索引
在select窗口中,执行以下语句: set profiling =1; -- 打开profile分析工具show variables like '%profil%'; -- 查看是否生效show p ...