ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

csrf相关装饰器、基于中间件思想编写项目、auth认证模块、bbs项目

2022-05-25 22:01:46  阅读:162  来源: 互联网

标签:send 中间件 module auth csrf msg import


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())

image-20220525155054991

image-20220525155126503

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('快要解封啦')

image-20220525173304931

方式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中添加即可

image-20220525173653650

auth认证模块

# django提供给你快速完成用户相关功能的模块
  用户相关功能:创建、认证、编辑、、、

# django配置提供了一张用户表
  执行数据库迁移命令之后默认产生的auth_user

# django自带的admin后台管理用户登录参考的就是auth_user
  创建admin后台管理员用户: run manage.py task>>:createsuperuser
  自动对用户密码进行加密处理并保存

image-20220525174731743

image-20220525175541037

image-20220525175753929

  • 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模块所有的方法都可以直接在自定义模型类上面使用
	自动切换参照表
"""

image-20220525213946464

bbs项目表设计

# 项目开发流程
1.需求分析
2.技术选型
3.分组开发
4.提交测试
5.交付上线

# 分析
"""
1.先确定表
2.再确定字段
3.最后确定关系
"""
1.用户表
	继承AbstractUser
2.个人站点表
	站点名称、标题、样式
3.文章表
	标题、简介、内容、发布时间
4.文章分类表
	分类名称
5.文章标签表
	标签名称
6.文章点赞点踩表
	文章、用户、赞/踩
7.文章评论表
	文章、用户、评论内容、评论时间

图片

标签:send,中间件,module,auth,csrf,msg,import
来源: https://www.cnblogs.com/lsw8898/p/16311096.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有