【问题标题】:linking users in the comments with Django用 Django 链接评论中的用户
【发布时间】:2017-10-01 22:52:24
【问题描述】:

你好社区我正在使用 Django,我想在评论中“标记”或“链接”用户(就像你在 Facebook 或 Reddit 中所做的那样)。目标应该是用户可以写评论并写一个“#”或“@”来引用评论中的用户,这个用户应该得到某种 f 通知。

我想尝试使用正则表达式(例如注释中的“#”...),因此用户名是个人资料的链接,但即使我让它工作,我也不知道如何发送用户他被提及的消息/通知。

有什么建议可以解决这个问题吗?我感觉有点迷失在这里,因为我不知道从哪里开始。

【问题讨论】:

    标签: django tags comments


    【解决方案1】:

    没有看到你的代码,你可以做这样的事情,使用 pythons split 并将其放在注释模型保存方法中

    hashtags = comment_string.split('#')
    mentioned_usernames = [word[0] for words in hashtags.split(' ', 1)]
    
    for username in mentioned_usernames:
        #get the user
        user = User.objects.get(username=username)
        #call your function that sends the notification to the user
        send_notification(user)
    

    【讨论】:

    • 我选择了这个,它在后端运行良好。现在我正在做前端部分...def userTagComment(self): tagUsers = re.findall(r"@+(\w+)", self.content) taggedUsers = list() for username in tagUsers: try: user = User.objects.get(username=username) taggedUsers.append(user) except: user = "franz" notitification(user) return taggedUsers
    猜你喜欢
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-08
    • 2012-03-05
    相关资源
    最近更新 更多