python manage.py createsuper
Username (leave blank to use 'administration'): admin
Email address: 123@qq.com
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
默认数据保存在数据库
所以需要提前执行迁移数据库命令
数据内容
项目启动
登录成功页面
User
) 和组 (Group
)用户管理:
组管理:
在组管理界面,可以查看、创建、修改和删除组。
可以查看一个组的成员,或者将用户添加到组中,或者从组中移除用户。
还可以为组分配权限。这些权限决定了组的成员可以访问和修改哪些资源。
组是一种方便的方式来管理一组有相同权限的用户
权限管理:
Article
) 模型,可能有 “添加文章”、“修改文章” 和 “删除文章” 等权限。from django.contrib import auth
auth.authenticate(request, username, password)
User
对象None
。auth.login(request, user_obj)
request.user
来获取当前的用户request.user
AnonymousUser
,匿名用户,是个对象request.user.is_authenticated
True
False
@login_required(login_url)
用于确保视图函数只能被已经登录的用户访问
如果用户没有登录,它将重定向到登录页面
登录成功将返回原始界面
跳转-局部配置
login_url=‘/login/‘
跳转-全局配置
LOGIN_URL = '/login/'
优先级
from django.contrib import auth
def login(request):
if request.method == "POST":
username = request.POST.get("username")
password = request.POST.get("password")
# 验证用户名和密码,验证失败是None
user_obj = auth.authenticate(request, username=username, password=password)
if user_obj:
# 验证通过,登录用户,将user_obj保存到request中
auth.login(request, user_obj)
# 跳转会原来的界面
next_url = request.GET.get("next", "/home/")
return redirect(next_url)
return render(request, 'login.html', locals())
def check(request):
print(request.user, type(request.user))
# AnonymousUser <class 'django.utils.functional.SimpleLazyObject'>
# admin <class 'django.utils.functional.SimpleLazyObject'>
print(request.user.is_authenticated)
return HttpResponse()
from django.contrib.auth.decorators import login_required
@login_required
def func2(request):
return HttpResponse("func2")
# http://127.0.0.1:8000/login/?next=/func1/
from django.contrib.auth.models import User
User.objects.create()
User.objects.create_user()
User.objects.create_superuser()
from django.contrib.auth.hashers import make_password, check_password
is_right = request.user.check_password(old_password)
request.user.set_password(new_password)
save()
方法make_password(password)
auth.logout(request)
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import User
def register(request):
if request.method == "POST":
username = request.POST.get('username')
password = request.POST.get('password')
confirm_password = request.POST.get('confirm_password')
# 密码加密
print(make_password(password))
if confirm_password == password:
# 创建普通用户,密码是明文
# User.objects.create(username=username, password=password)
# 创建普通用户,密码经过加密了
User.objects.create_user(username=username, password=password)
# 创建管理员用户
# User.objects.create_superuser(username=username, password=password)
return render(request, 'register.html')
from django.contrib.auth.decorators import login_required
from django.contrib.auth.hashers import make_password, check_password
@login_required(login_url='/login/')
def revise_password(request):
user_obj = request.user
if request.method == "POST":
old_password = request.POST.get('old_password')
new_password = request.POST.get('new_password')
confirm_password = request.POST.get('confirm_password')
if new_password == confirm_password:
# 判断原始密码时候正确
is_right = request.user.check_password(old_password)
if is_right:
# 设置新密码
request.user.set_password(new_password)
request.user.save()
return render(request, 'revise_password.html', locals())
<form action="" method="post">
<p>username:<input type="text" name="username" disabled value="{{ user_obj.username }}"></p>
<p>old_password:<input type="password" name="old_password"></p>
<p>new_password:<input type="password" name="new_password"></p>
<p>confirm_password:<input type="password" name="confirm_password"></p>
<button class="button">revise</button>
</form>
from django.contrib.auth.decorators import login_required
@login_required
def logout(request):
username = request.user.username
auth.logout(request)
return HttpResponse(f"{username}退出")
# bruce退出
from django.db import models
from django.contrib.auth.models import User
class UserInfo(models.Model):
addr = model.CharField(max_length=255)
user = models.OneToOneField(to='User', on_delete=models.CASCADE)
AbstractUser
前提:
注意事项
新表内不要创建已有的字段
需要修改配置文件settings
AUTH_USER_MODEL = '应用名.表名'
使用的时候需
from django.db import models
from django.contrib.auth.models import User, AbstractUser
class UserInfo(AbstractUser):
addr = models.CharField(max_length=255)
更多【python-Django auth模块】相关视频教程:www.yxfzedu.com