csrf相关装饰器、基于中间件思想编写项目、auth认证模块、bbs项目
2022/5/25 23:22:42
本文主要是介绍csrf相关装饰器、基于中间件思想编写项目、auth认证模块、bbs项目,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
csrf相关装饰器
基于中间思想编写项目
auth认证模块
bbs项目表设计
csrf相关装饰器
from django.views.decorators.csrf import csrf_exempt, csrf_protect @csrf_protect # 忽略csrf校验 @csrf_exempt # 开启csrf校验 FBV(基于函数的视图): @csrf_exempt/@csrf_protect def login(request): if request.method == 'POST': username = request.POST.get('username') if username == 'gavin': next_path = request.GET.get('next') if next_path: res = redirect(next_path) else: res = redirect('/home/') res.set_signed_cookie('name', 'gavin', salt='加密盐') return res return render(request, 'login.html', locals())
CBV(基于类的视图): from django.utils.decorators import method_decorator # 方式一: 在类中的某个方法上面添加 # 方式二: 直接在类名中添加并指定 # 方式三: 重写dispatch方法并添加作用于类中所有的方法(全局) csrf_protect 这三种CBV添加装饰器的方式都可以使用 csrf_exempt 只有一种方式可以生效(给重写的dispatch方法装)
基于中间思想编写项目
# importlib模块 可以通过字符串的形式导入模块 # 常规的导入方式 from ccc import b print(b) # 字符串导入方式 import importlib module_path = 'ccc.b' res = importlib.import_module(module_path) print(res.name) # importlib导入最小导入单位是模块文件级别 from ccc.b import name import importlib module_path = 'ccc.b.name' res = importlib.import_module(module_path) print(res) # 报错 No module named 'ccc.b.name'; 'ccc.b' is not a package
'''以发送提示信息为需求, 编写功能''' 方式一:封装成函数 def send_email(msg): print('邮箱信息提示:%s'%msg) def send_msg(msg): print('短信信息提示:%s'%msg) def send_qq(msg): print('qq信息提示: %s'%msg) def send_all(msg): send_msg(msg) send_qq(msg) send_email(msg) # start文件 from notify import send_all send_all('快要解封啦')
方式2:封装成配置 # 单个文件中 class Email(object): def __init__(self): pass # 模拟发送邮件 def send(self, msg): print('邮箱信息:%s'%msg) # settings文件中 NOTIFY_FUNC_LIST = [ 'notify.email.Email', 'notify.qq.QQ', 'notify.msg.Msg' ] # __init__ import settings import importlib def send_all(msg): # 循环获取配置文件中字符串信息 for str_path in settings.NOTIFY_FUNC_LIST: # 切割路径信息 module_path, class_str_name = str_path.rsplit('.', maxsplit=1) # 根据module_path导入模块文件 module = importlib.import_module(module_path) # 利用反射获取模块文件中对应的类名 class_name = getattr(module, class_str_name) # 实例化 obj = class_name() # 调用发送消息的功能 obj.send(msg) # 想要再往里面添加功能类,在settings中添加即可
auth认证模块
# django提供给你快速完成用户相关功能的模块 用户相关功能:创建、认证、编辑、、、 # django配置提供了一张用户表 执行数据库迁移命令之后默认产生的auth_user # django自带的admin后台管理用户登录参考的就是auth_user 创建admin后台管理员用户: run manage.py task>>:createsuperuser 自动对用户密码进行加密处理并保存
-
auth模块方法大全
from django.contrib import auth # 模块导入 # 1.验证用户名和密码是否正确 auth.authenticate() is_user_obj = auth.authenticate(request, username=username, password=password) # 校验成功,返回对象 # 2.保存用户登录状态 auth.login() # 用户名和密码正确,自动添加在session添加记录 # 3.获取当前用户对象 request.user # 4.判断当前用户是否登录 request.user.is_authenticated() # True 或 Flase # 5.校验登录装饰器 from django.contrib.auth.decorators import login_required @login_required(login_url='/lg/') # 局部配置 @login_required # 全局配置 LOGIN_URL = '/lg/' # 需要在配置文件中添加配置 # 6.修改密码 request.user.check_password() # 自动加密然后比对 request.user.set_password() # 修改密码 request.user.save() # 保存 # 7.注销登录 auth.logout(request) # 删除session记录 # 8.注册用户 from django.contrib.auth.models import User User.objects.create_superuser() # 管理员用户 User.objects.create_user() # 普通用户 # User.objects.create_user(username='tom1', password='tom123456') # User.objects.create_superuser(username='tom', password='tom123456', email='123@qq.com')
-
auth扩展表字段
# 方法一: 编写一对一表关系 # 方式2:类继承(推荐) from django.contrib.auth.models import AbstractUser class Users(AbstractUser): phone = models.BigIntegerField() addr = models.CharField(max_length=32) # 配置文件增加 提示auth模块, 不在使用auth_user, 使用自定义的表 AUTH_USER_MODEL = 'app01.User' """ 1.类继承之后 需要重新执行数据库迁移命令 并且库里面是第一次操作才可以 2.auth模块所有的方法都可以直接在自定义模型类上面使用 自动切换参照表 """
bbs项目表设计
# 项目开发流程 1.需求分析 2.技术选型 3.分组开发 4.提交测试 5.交付上线 # 分析 """ 1.先确定表 2.再确定字段 3.最后确定关系 """ 1.用户表 继承AbstractUser 2.个人站点表 站点名称、标题、样式 3.文章表 标题、简介、内容、发布时间 4.文章分类表 分类名称 5.文章标签表 标签名称 6.文章点赞点踩表 文章、用户、赞/踩 7.文章评论表 文章、用户、评论内容、评论时间
这篇关于csrf相关装饰器、基于中间件思想编写项目、auth认证模块、bbs项目的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升