慕课网-Django入门到进阶-更适合Python小白的系统课程-第3章Django中的Template模板-3-9项目练习-Django模板实践与枚举学习、消息屏蔽
第3章 Django中的Template模板
3-9 项目练习-Django模板实践与枚举学习、消息屏蔽
1.进入项目 message_test 下目录 app,修改文件 views.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.views.generic import View
from django.http import HttpResponse
class LessionThree(View):
def get(self, request, message_type):
# message = request.GET.get('message', '这里没有内容')
return HttpResponse(message_type)
2.进入项目 message_test 下目录 message_test,修改文件 urls.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.urls import path
from .views import LessionThree
urlpatterns = [
path('three/<str:message_type>', LessionThree.as_view(), name='three')
]
3.进入项目 message_test 下目录 app,添加文件 consts.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from enum import Enum
class MessageType(Enum):
info = 'info'
warning = 'warning'
error = 'error'
danger = 'danger'
4.在命令窗口进行调试
python manage.py shell from app.consts import MessageType MessageType.info MessageType.info.value
5.进入项目 message_test,添加目录 templates
6.进入项目 message_test 下目录 templates,添加文件 three.html
7.进入项目 message_test 下目录 message_test,修改文件 settings.py
#修改模板路径
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR + '/templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
8.进入项目 message_test 下目录 app,修改文件 views.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from django.views.generic import View
from django.shortcuts import render
#from django.http import HttpResponse
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 render(request, self.TEMPLATE, data)
9.进入项目 message_test 下目录 app,修改文件 consts.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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'
10.进入项目 message_test 下目录 templates,修改文件 three.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if error %}
<h3>error:{{error}}</h3>
{% else %}
<span style="color:{{message_type.color}}">{{message}}</span>
{% endif %}
</body>
</html>
11.测试


12.进入项目 message_test 下目录 templates,修改文件 three.html
<!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}}</span>
{% endif %}
</body>
</html>
13.进入项目 message_test 下目录 app,修改文件 consts.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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 = ['天气', '坏人', '不开心']
14.在项目 message_test 下目录 app,新建目录 templatetags
15.在项目 message_test 下目录 templatetags,新建文件 custom.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import jieba
from django import template
from app.consts import SensitiveWord
register = template.Library()
@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
16.进入项目 message_test 下目录 message_test,修改文件 settings.py
#添加应用
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
'app.templatetags'
]
17.进入项目 message_test 下目录 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|sample_check}}</span>
{% endif %}
</body>
</html>
18.测试

19.在项目 message_test 下目录 templatetags,修改文件 custom.py,只替换违禁词汇
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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
20.进入项目 message_test 下目录 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}}</span>
{% endif %}
</body>
</html>
21.测试

22.在项目 message_test 下目录 templatetags,修改文件 custom.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
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)
22.进入项目 message_test 下目录 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>
23.测试

posted on 2020-01-24 05:42 herisson_pan 阅读(19) 评论(0) 收藏 举报
浙公网安备 33010602011771号