如何在 django 开发的项目中使用“@"提及/标记用户 [英] how to mention/tag users with '@' on a django developed project

查看:25
本文介绍了如何在 django 开发的项目中使用“@"提及/标记用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现在社交网站(例如 twitter)上使用的@"功能,以在我的 django 项目中标记或提及用户.例如,单击@stack"时应转到堆栈的个人资料.

如何做到这一点对我有帮助.

解决方案

在编辑器上处理的提及系统对吗?这是 django-markdown-editor 谁提供直接提及用户 @[username] => @username

另见关于函数markdown_find_mentions,如果您需要为其他用户提到的用户实现通知系统,例如stackoverflow,则很有用.

def markdown_find_mentions(markdown_text):"""找到提到的用户关于使用BeautifulShoup"的降价内容.输入:`markdown_text` 或降价内容.返回:用户名的`list`."""标记 = markdownify(markdown_text)汤 = BeautifulSoup(mark, 'html.parser')返回列表(设置(username.text[1::] 用于用户名汤.findAll('a', {'class': 'direct-mention-link'})))

这是一个简单的流程;

  1. 在创建评论/帖子/等时,找到所有提及的用户并创建通知.
  2. 编辑评论/帖子/等时,找到所有提到的新用户并创建通知.

<块引用>

确保通知有发送者和接收者.

类通知(TimeStampedModel):sender = models.ForeignKey(用户,related_name='sender_n')接收者 = 模型.ForeignKey(用户,related_name='receiver_n')content_type = models.ForeignKey(ContentType, related_name='n', on_delete=models.CASCADE)object_id = models.PositiveIntegerField()content_object = GenericForeignKey('content_type', 'object_id')读取=models.BooleanField(默认=假)....

I am trying to implement the "@" feature used on social sites such as twitter to tag or mention users on my django project. Say for example, "@stack" when clicked should go to stack's profile.

How to do that would be helpful to me.

解决方案

The mention system handled on editor right? Here is django-markdown-editor who providing to direct mention user @[username] => @username

see also about function markdown_find_mentions, useful if you need to implement the notification system for user mentioned by another users, something like stackoverflow.

def markdown_find_mentions(markdown_text):
    """
    To find the users that mentioned
    on markdown content using `BeautifulShoup`.

    input  : `markdown_text` or markdown content.
    return : `list` of usernames.
    """
    mark = markdownify(markdown_text)
    soup = BeautifulSoup(mark, 'html.parser')
    return list(set(
        username.text[1::] for username in
        soup.findAll('a', {'class': 'direct-mention-link'})
    ))

and this a simply flow process to do;

  1. When create a comment/post/etc, find all users mentioned and create a notification.
  2. When edit a commemnt/post/etc, find all new users mentioned and create a notification.

Makesure the Notification have a sender and receiver.

class Notification(TimeStampedModel):
    sender = models.ForeignKey(User, related_name='sender_n')
    receiver = models.ForeignKey(User, related_name='receiver_n')
    content_type = models.ForeignKey(ContentType, related_name='n', on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    read = models.BooleanField(default=False)

    ....

这篇关于如何在 django 开发的项目中使用“@"提及/标记用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆