django入门到精通⑥消息管理器的升级处理,对关键词进行过滤示例
# 安装分词工具
(python37_django2) D:pythonmessage_test>pip install jieba
设置message_test/settings.py
1.自定义分词工具
app/templatetags/custom.py
# _*_ coding:utf-8 _*_ # __author__ == 'jack' # __date__ == '2020-12-31 9:03 PM' import jieba from django import template from app.consts import SensitiveWord register = template.Library() @register.filter(name='deep_check_message') def deep_check(value): # 只屏蔽关键词,替换为 * 号 cut_message = jieba.lcut(value) new_message = [] for m in cut_message: if m in SensitiveWord: new_message.append('*') else: new_message.append(m) if new_message: return ''.join(new_message) return value @register.filter def sample_check(value): # 自定义的关键词过滤器 cut_message = jieba.lcut(value) print(cut_message) print(SensitiveWord) check = list(set(cut_message) & set(SensitiveWord)) if len(check) != 0: return '该消息涉及违禁词汇,已被屏蔽' return value @register.filter def add_message_year(value, year): return '{} {}'.format(value, year)
2.自定义关键词
app/consts.py
# _*_ coding:utf-8 _*_ # __author__ == 'jack' # __date__ == '2020-12-31 8:28 PM' from enum import Enum class MessageType(Enum): info = "info" warning = "warning" error = "error" danger = "danger" MessageType.info.label = '信息' MessageType.warning.label = '警告' MessageType.error.label = '错误' MessageType.danger.label = '危险' MessageType.info.color = 'green' MessageType.warning.color = 'orange' MessageType.error.color = 'gray' MessageType.danger.color = 'red' SensitiveWord = ['天气', '坏人', '不开心']
3.视图
app/views.py
# coding:utf-8 from django.views.generic import View # from django.http import HttpResponse from django.shortcuts import render from .consts import MessageType class LessionThree(View): TEMPLATE = 'three.html' def get(self, request, message_type): data = {} try: message_type_obj = MessageType[message_type] except Exception as e: data['error'] = '没有这个消息类型{}'.format(e) return render(request, self.TEMPLATE, data) message = request.GET.get('message', '') if not message: data['error'] = '消息不能为空' return render(request, self.TEMPLATE, data) data['message'] = message data['message_type'] = message_type_obj # return HttpResponse(message_type) return render(request, self.TEMPLATE, data)
5.自定义模板html页面
templates/three.html
{% load custom %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% if error %} <h3>error {{ error }}</h3> {% else %} <label style="color:{{ message_type.color }}">{{ message_type.label }}</label> <span style="color:{{ message_type.color }}">{{ message|deep_check_message|add_message_year:2020 }}</span> {% endif %} </body> </html>
6.路由信息
app/urls.py
# _*_ coding:utf-8 _*_ # __author__ == 'jack' # __date__ == '2020-12-29 8:27 PM' from django.urls import path from .views import LessionThree urlpatterns = [ path('three/<str:message_type>', LessionThree.as_view(), name="two") ]
总路由 message_test/urls.py
from django.contrib import admin from django.urls import path,include from app import urls as app_urls urlpatterns = [ path('admin/', admin.site.urls), path('', include(app_urls)) ]
测试