一,概述:

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(用户认证)的更多相关文章

  1. django用户认证系统——拓展 User 模型

    Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...

  2. django用户认证系统——拓展 User 模型2

    Django 用户认证系统提供了一个内置的 User 对象,用于记录用户的用户名,密码等个人信息.对于 Django 内置的 User 模型, 仅包含以下一些主要的属性: username,即用户名 ...

  3. Django——用户认证

    Django--用户认证 用户与Authentication(身份验证) Django 用户认证系统处理用户帐号,组,权限以及基于cookie的用户会话. 这个系统一般被称为 auth/auth (认 ...

  4. [django]用户认证中只允许登陆用户访问(网页安全问题)

    当设计一个重要网页时,一般要求未从登陆界面访问的用户不能进入其他页面,那么需要如何设置呢? 如下 django中的url.py urlpatterns = [    url(r'^$', 'login ...

  5. Django 用户认证及OneToOneField

    Django 用户认证如果自己不想写 就可以用django自带的认证 首选导入模块 models.py #!/usr/bin/env python #_*_ coding:utf8 _*_ from ...

  6. “Django用户认证系统”学习资料收集

    首推追梦人物——Django用户认证系统 待续……

  7. Django 2.0 学习(17):Django 用户认证(auth模块)

    Django 用户认证(auth模块) 一.认证登陆 在进行用户登陆验证的时候,如果是自己写代码,就必须要先查询数据库,看用户输入的用户名是否存在于数据库中:如果用户存在于数据库中,然后再验证用户输入 ...

  8. django用户认证系统——重置密码7

    当用户不小心忘记了密码时,网站需要提供让用户找回账户密码的功能.在示例项目中,我们将发送一封含有重置用户密码链接的邮件到用户注册时的邮箱,用户点击收到的链接就可以重置他的密码,下面是具体做法. 发送邮 ...

  9. django用户认证系统——修改密码6

    再此之前我们已经完成了用户登录.注册.注销等功能,接下来让我们继续为用户提供修改密码的功能.该功能 Django 的 auth 应用也已经为我们提供,过程几乎和之前的登录功能完全一样. 编写修改密码模 ...

随机推荐

  1. html基础学习笔记1

    <!DOCTYPE html> 声明为 HTML5 文档  <html> 元素是 HTML 页面的根元素 <head> 元素包含了文档的元(meta)数据,如 &l ...

  2. 16路PWM输出的pca9685模块

    今天要介绍的就是该模块,该模块是16路pwm模块,使用I2C总线可以控制16路舵机(led). 接线OE空着就可以,其他VCC是芯片供电+5,SCL时钟线,SDA信号线,GND地线. 芯片介绍可以看: ...

  3. 12 个 JS 技巧

    1. 过滤唯一值 ES6 引入了 Set 对象和延展(spread)语法…,我们可以用它们来创建一个只包含唯一值的数组. 复制代码     const array = [1, 1, 2, 3, 5, ...

  4. windows的github教程

    如何向github提交变更1. 在github上面创建project,获得git地址:2. 来到windows,首先安装git:3. 在目标目录下:git clone https://github.c ...

  5. 【TensorFlow】tfdbg调试注意事项

    按照网上的帖子开启tfdbg调试,可能因为没有安装curses和pyreadline包导致失败. 运行 python test001.py --debug 报错: ModuleNotFoundErro ...

  6. vs单独调试dll

    用生成好的exe单独调试dll,右键项目属性->调试->命令->选择exe

  7. 使用 random() 生成礼包码

    需求:生成100个游戏礼包码 具体如下 # 1.构造100个5位随机数 listNumA = [] for n in range(100): listNumA.append(random.randin ...

  8. Linux下MySQL编码的修改

    默认登录mysql之后可以通过SHOW VARIABLES语句查看系统变量及其值. mysql> show variables like '%character%';   说明:以下是在Cent ...

  9. Selenium之动作链(ActionChains)

    用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件——ActionChains   se ...

  10. android全屏/沉浸式状态栏下,各种键盘挡住输入框解决办法

    https://blog.csdn.net/smileiam/article/details/69055963