在Django开发的项目中如何用'@'提及/标记用户 [英] how to mention/tag users with '@' on a django developed project
问题描述
我正在尝试实现在诸如twitter之类的社交网站上使用的 @功能,以在django项目中标记或提及用户。举例来说,单击 @stack应该转到堆栈的个人资料。
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.
推荐答案
在编辑器中处理的提及系统正确吗?这是 django-markdown-editor 谁提供直接提及用户 @ [用户名]
=> @username
The mention system handled on editor right? Here is django-markdown-editor who providing to direct mention user @[username]
=> @username
另请参见函数 markdown_find_mentions
,如果您需要为其他用户提到的用户实施通知系统,例如stackoverflow,则很有用。
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'})
))
,这是一个简单的流程; p>
and this a simply flow process to do;
- 创建评论/帖子/等时,找到提到的所有用户并创建通知。
- 何时编辑commemnt / post / etc,查找提到的所有新用户并创建通知。
通知具有发送者和接收者。
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屋!