how to mention/tag users with '@' on a django developed project

给你一囗甜甜゛ 提交于 2020-01-22 16:58:25

问题


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.


回答1:


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)

    ....


来源:https://stackoverflow.com/questions/46930987/how-to-mention-tag-users-with-on-a-django-developed-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!