一、补充知识点importlib
- importlib可以通过字符串的形式导包,常用于settings全局启动时使用,但是需要注意最小单位只能到模块名
import importlib
res = 'myfile.b'
ret = importlib.import_module(res)
print(ret)
- 功能的插拔式设计
- ① 配置文件注册功能
- ② importlib模块
- ③ 字符串切割split
- ④ 反射
- ⑤ 面向对象及鸭子类型
import settings
import importlib
def send_all(content):
for path_str in settings.NOTIFY_LIST:
module_path, class_name = path_str.rsplit('.', maxsplit=1)
module = importlib.import_module(module_path)
cls = getattr(module, class_name)
obj = cls()
obj.send(content)
二、Auth模块
- django推荐
- 其实我们在创建好一个django项目只会直接执行数据库迁移命令会自动生成很多表,django_session,auth_user;
- django在启动只会就可以直接访问admin路由,需要输入用户名和密码,数据参考的就是auth_user表,并且还必须式管理用户才能进入;
- auth_user表添加用户,创建超级用户(管理员)
python3 manage.py createsuperuser
依赖于auth_user表完成用户相关的所有功能 - 方法1:
比对密码是否正确
user_obj = auth.authenticate(request, username=username, password=password)
print(user_obj)
print(user_obj.username)
print(user_obj.password)
- 方法2:
保存用户状态
auth.login(request, user_obj)
- 方法3:
判断当前用户是否登录
print(request.user.is_authenticated)
- 方法4:
获取当前登录用户
request.user
- 方法5:
校验用户是否登录装饰器
from django.contrib.auth.decorators import login_required
@login_required(login_url='/login/')
@login_required
- 方法6:
比对原密码
request.user.check_password(old_password)
- 方法7:
修改密码
request.user.set_password(new_password)
request.user.save()
- 方法8:
注销
auth.logout(request)
- 方法9:
注册
User.objects.create_user(username=username, password=password)
```
- auth_user表扩展之models.py; settings.py配置AUTH_USER_MODEL = ‘app03auth.UserInfo’ (即应用名.表名)
from django.db import models
from django.contrib.auth.models import User, AbstractUser
class UserInfo(AbstractUser):
"""
如果继承了AbstractUser
那么在执行数据库迁移命令的时候auth_user表就不会再创建出来了
而UserInfo表中会出现auth_user所有的字段外加自己扩展的字段
这么做的好处在于你能够直接点击你自己的表更加快速的完成操作及扩展
前提:在继承之前没有执行过数据库迁移命令, auth_user没有被创建,如果已经创建那么就重新换一个库
继承的类里面不要覆盖AbstractUser里面的字段名
表里面的字段都不要动,只扩展就行
需要在配置文件中告诉django你要用UserInfo替代auth_user : AUTH_USER_MODEL = 'app03auth.UserInfo' (即应用名.表名)
如果自己的表替代了auth_user,那么auth模块的功能还是照常使用,参考的表也由原来的auth_user变成了UserInfo
"""
phone = models.BigIntegerField()
create_time = models.DateTimeField(auto_now_add=True)
urls.py
from django.conf.urls import url
from django.contrib import admin
from app03auth import views
urlpatterns = (
url(r'^admin/', admin.site.urls),
url(r"^login/", views.login),
url(r"^home/", views.home),
url(r"^index/", views.home),
url(r"^set_password/", views.set_password),
url(r"^logout/", views.logout),
url(r"^register/", views.register)
)
views.py
from django.shortcuts import render, redirect, HttpResponse
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
def login(request):
"""使用auth模块要用就用全套"""
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user_obj = auth.authenticate(request, username=username, password=password)
print(user_obj)
if user_obj:
auth.login(request, user_obj)
return redirect('/app03/home/')
return render(request, 'login.html')
@login_required
def home(request):
"""用户登录之后才能看home"""
print(request.user)
print(request.user.is_authenticated)
return HttpResponse('OK')
@login_required
def index(request):
return HttpResponse('login_url')
@login_required
def set_password(request):
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 redirect('/app03/login/')
return render(request, 'set_password.html', locals())
@login_required
def logout(request):
auth.logout(request)
return redirect('/app03/login/')
def register(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
User.objects.create_user(username=username, password=password)
return render(request, "register.html")
set_password.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
{% csrf_token %}
<p>username:<input type="text" name="username" class="form-control" disabled value="{{ request.user.username }}"></p>
<p>old_password:<input type="text" name="old_password" class="form-control"></p>
<p>new_password:<input type="text" name="new_password" class="form-control"></p>
<p>confirm_password:<input type="text" name="confirm_password" class="form-control"></p>
<input type="submit" class="btn btn-success btn-block">
</form>
</body>
</html>
login.html与register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆</title>
{% load static %}
<script src="{% static 'bootstrap-3.3.7-dist/js/jquery-3.5.1.min.js' %}"></script>
<script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}"></script>
<link rel="stylesheet" href= "{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}">
</head>
<body>
<form action="" method="post">
{% csrf_token %}
<h1>注册</h1>
<p>username:<input type="text" name="username" class="form-control"></p>
<p>password:<input type="password" name="password" class="form-control"></p>
<input type="submit" class="btn btn-success btn-block">
</form>
</div>
</body>
</html>