media配置
1.作用:
1.规定静态文件上传存放路径(路径配置,文件夹不存在会自动创建)
2.可以将任意的后端资源暴露给用户(路由配置, 慎用)。
2.场景:
网站所使用的静态文件默认都是放在static文件夹下
用户上传的静态文件 也应该放在一个固定的文件夹下
我们目前实现的是用户上传的头像 固定放在avatar文件夹下了
我们应该创建一个类似于static文件夹 里面在根据文件的不同创建不同的文件夹存储数据3.具体使用(以暴露图片资源和应用app01的资源)
1.现在settings.py文件中路径配置 # 规定 用户上传的所有的静态文件 全部放到media文件夹下,可更改文件名('media'),文件不存在会自动创建 MEDIA_ROOT = os.path.join(BASE_DIR,'media') # 暴露任意文件夹资源(暴露app01下的组员) # MEDIA_ROOT1 = os.path.join(BASE_DIR,'app01') 2.urls.py文件中,配置路由暴露资源 #导入固定模块 from django.views.static import serve # 导入项目名下的settings.py文件 以BBS为例 from BBS import settings # 手动暴露后端文件夹资源(settings.MEDIA_ROOT为第1步中的setting.py中的配置,根据实际情况来设置) url(r'^media/(?P<path>.*)',serve,{"document_root":settings.MEDIA_ROOT}), # 手动暴露后端文件资源的时候 一定要慎重 # url(r'^app01/(?P<path>.*)',serve,{"document_root":settings.MEDIA_ROOT1})
XSS攻击处理
1.简单来说就是不让<script></script>标签在html源编辑器,中有标签功能,防止他的恶意攻击循环。(例如用户在博客园中编写文章利用html编辑<script></script>语言时,<script>语言起到显示效果这就是xss攻击。)
2.要解决的问题:
#1.文章简介的获取
截取150个中文字符,不包含文章中html标签
#2.防止用户写script脚本
1.获取用户输入的所有的script标签直接删除
2.给script转义
3.解决方案:模块:beautifulsoup4简称 BS4
https://www.cnblogs.com/Dominic-Ji/p/9637705.html
简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据。官方解释如下: ''' Beautiful Soup提供一些简单的、python式的函数用来处理导航、搜索、修改分析树等功能。 它是一个工具箱,通过解析文档为用户提供需要抓取的数据,因为简单,所以不需要多少代码就可以写出一个完整的应用程序。 '''
#1.安装(2中方式)
pip3 install beautifulsoup4

#2.简单实用
1.导入模块 from bs4 import BeautifulSoup 2.先生成一个BeautifulSoup对象 soup = BeautifulSoup(content,'html.parser') # 要处理的内容,解析器 3.利用for循环拿到内容中所有的标签:soup.find_all()标签对象集合 tag.name标签对象名字 for tag in soup.find_all(): tag.name # 内容一个个标签 tag.decompose() # 标签删除 4.拿到内容中的正常文本(字符串) soup.text # desc = soup.text[0:150] # 文本内容切片


from bs4 import BeautifulSoup @login_required def add_article(request): if request.method == 'POST': title = request.POST.get('title') content = request.POST.get('content') tags = request.POST.getlist('tag') category_id = request.POST.get('category') # 麻瓜式做法 直接对content窃取150 # 1 先生成一个BeautifulSoup对象 soup = BeautifulSoup(content,'html.parser') for tag in soup.find_all(): # 针对script标签 应该直接删除 # print(tag.name) # 获取当前html页面所有的标签 if tag.name == 'script': tag.decompose() # 将符合条件的标签删除 # 文章简介应该是150个文本内容 desc = soup.text[0:150] # desc = content[0:150] article_obj = models.Article.objects.create(title=title,desc=desc,content=str(soup),category_id=category_id,blog=request.user.blog) # 一个个的添加 b_list = [] for tag_id in tags: b_list.append(models.Article2Tag(article=article_obj,tag_id=tag_id)) models.Article2Tag.objects.bulk_create(b_list) return redirect('/backend/') tag_list = models.Tag.objects.filter(blog=request.user.blog) category_list = models.Category.objects.filter(blog=request.user.blog) return render(request,'backend/add_article.html',locals())
随机生成验证码
PIL模块
1.下载模块 pip3 install pillow 2.导入模块 from PIL import Image,ImageDraw,ImageFont 3.使用 """ Image 生成图片的 ImageDraw 在图片上写东西的 ImageFont 控制字体样式的 """
io模块
from io import BytesIO,StringIO """ io是一个内存管理器模块 BytesIO 能够帮你存储数据 二级制格式 StringIO 能够帮你存储数据 字符串格式 """


# 随机数字 import random def get_random(): return random.randint(0,255), random.randint(0,255),random.randint(0,255) # 验证码 from PIL import Image,ImageDraw,ImageFont from io import BytesIO,StringIO def get_code(request): # 图片对象(颜色格式,位置,颜色) img_obj = Image.new('RGB',(360,35),get_random()) # 画笔对象 image_draw_obj = ImageDraw.Draw(img_obj) # 图片字体 img_font_obj = ImageFont.truetype('static/font/111.ttf',30) # 内存对象 io_obj = BytesIO() # 产生5位验证码 code = '' for i in range(5): # 大写小写 数字 d = chr(random.randint(65,90)) x = chr(random.randint(97,122)) n = str(random.randint(0,9)) z = random.choice([d,x,n]) code += z # 朝图片书写(位置,内容,颜色,字体) image_draw_obj.text((70+i*45,0),z,get_random(),font=img_font_obj) # 将验证码保存起来 将图片存放到内存中 request.session['code'] = code img_obj.save(io_obj,'png') # 位置 格式 return HttpResponse(io_obj.getvalue()) # 登录 def login(request): if request.method == 'POST': back_dic = {'code':100,'msg':''} username = request.POST.get('username') password = request.POST.get('password') # 用户输入的验证码 code1 = request.POST.get('code') # 浏览器原本显示的验证码 code2 = request.session.get('code') print(code2) # 校验验证码和用户名和密码 if code1.upper() == code2.upper(): # 判断有户名密码 user_obj = auth.authenticate(username=username,password=password) if user_obj: # 记录登录状态 auth.login(request,user_obj) back_dic['url'] = '/home/' back_dic['msg'] = '登录成功' else: back_dic['msg'] = '用户名或密码错误' back_dic['code'] = 101 else: back_dic['code'] = 102 back_dic['msg'] = '验证码错误' return JsonResponse(back_dic) return render(request,'login.html')
转载于:https://www.cnblogs.com/tfzz/p/11604454.html