def make_html_content_and_add_tags(self):
# ????? ???? ?????
p = re.compile(r'(#\w+)')
# findall???? ???? ????? ???
tag_name_list = re.findall(p, self.content)
# ?? content(Comment??)? ??? ??
ori_content = self.content
# ????? ????
for tag_name in tag_name_list:
# Tag??? ????? ??, ????? ???? ????? _??
tag, _ = Tag.objects.get_or_create(name=tag_name.replace('#', ''))
# ?? content? ??? ??
change_tag = '<a href="{url}" class="hash-tag">{tag_name}</a>'.format(
# url=reverse('post:hashtag_post_list', args=[tag_name.replace('#', '')]),
url=reverse('post:hashtag_post_list',
kwargs={'tag_name': tag_name.replace('#', '')}),
tag_name=tag_name
)
ori_content = re.sub(r'{}(?![<\w])'.format(tag_name), change_tag, ori_content, count=1)
# content? ??? Tag??? ??? tags??? ??
if not self.tags.filter(pk=tag.pk).exists():
self.tags.add(tag)
# ??? ??? ???? html_content? ??
self.html_content = ori_content
super().save(update_fields=['html_content'])
评论列表
文章目录