当前位置:网站首页>话题功能实现
话题功能实现
2022-07-28 05:20:00 【可宇龙】
相信大家对话题都了解吧,比如微博的话题,点击话题展示该话题下的所有帖子


主要技术栈就是把话题替换为一个超链接标签可以点击
下面代码展示(python,tornado框架实现)>>>>>>>>
# 正则替换
async def change_object(self, sublist, stype):
re_dict = {
"talk": {"re": r'#([\w]+)#', "model": TalkModel, "column": "TalkModel.name.in_(items)",
"getkey": "myid.name", "replace": "#{}#", "a": "<a href='/talk?tid={}'>#{}#</a>"}
}
myre = re_dict[stype]["re"]
for x in sublist:
# 替换话题
items_dict = {}
items = re.findall(myre, x["content"])
ids = await self.database.execute(
re_dict[stype]["model"].select().where(eval(re_dict[stype]["column"])))
for myid in ids:
items_dict[eval(re_dict[stype]["getkey"])] = myid.id
for item in items:
try:
x["content"] = x["content"].replace(re_dict[stype]["replace"].format(item),
re_dict[stype]["a"].format(items_dict[item], item))
except Exception as e:
print(str(e))
pass
return sublist此代码,利用了封装的思路,将替换封装为一个方法,以便调用,减少代码的复用!
替换后前端将获取到的替换内容使用v-html转换为标签即可,通过点击超链接获取到指定标签id,来获取该标签下的所有内容!
类似这种需要替换的还有好多,@好友也是同样原理,感兴趣的小伙伴可以去试试
边栏推荐
猜你喜欢
随机推荐
animation动画实现划过(点击)暂停
[uni app] the use of scroll into view in uni app
cmd和npm基础命令
【uni-app】uni-app中scroll-into-view的使用
null和undefined的区别
ArcMap连接表格(Join)相关问题整理
CMD and NPM basic commands
操作文档树
基于php小区疫情出入管理系统(php毕业设计)
标准C语言学习总结5
Microsoft Edge浏览器插件(2)
DOM操作的案例
图片根据屏幕自适应
异步编程Promise
结果填空 李白打酒(心态炸了)
Annotation and grid addition of ArcGIS map making
curd组件中的时间设置
Screenshot transferred to the background
结果填空 凑算式(DFS*C语言)
(php毕业设计)基于php学生日常行为管理系统获取









