【问题标题】:Return usernames mentioned in post as profile links to those users inside the post将帖子中提到的用户名作为个人资料链接返回到帖子中的那些用户
【发布时间】:2020-05-15 09:11:44
【问题描述】:

大家好,我正在尝试让我的代码检查数据库中带有@username 的帖子中提到的用户,并将该用户个人资料链接作为@username 返回。我目前能够在我的视图中使用 tagged_users 变量在后端获取用户。它存储提到的用户,但我如何在模板中访问它,以便它为我提供指向这些用户配置文件的链接?到目前为止,我的代码在模板中是这样的

{% if post.post|slice:":1" == '@' %}
   <a href="{% url 'profile_with_pk' pk=user.username %}">
     {{ post.post }}
   </a>
   {% else %}
   {{ post.post }}
   {% endif %}

这会将整个帖子作为链接返回,但不会返回帖子中提到的实际用户,而是会链接到当前登录的用户

这是我的代码,可以正确返回 tagged_users 列表中提到的用户我想在模板中访问它作为帖子中这些用户个人资料的链接。

def post(self, request, pk=None):
        if pk:
            user = User.objects.get(pk=pk)
        else:
            user = request.user
        form = HomeForm(request.POST)
        users = User.objects.all()
        if form.is_valid():
            post = form.save(commit=False)
            post.user = request.user
            post.save()
            tagUsers = re.findall(r"@+(\w+)", post.post)
            tagged_users = list()
            for username in tagUsers: 
                    user = User.objects.get(username=username) 
                    tagged_users.append(user)
                    print(tagged_users)
            text = form.cleaned_data['post']
            form = HomeForm()
            tag = tagged_users
            return redirect('home')


        context = {'form': form, 'text': text, 'users':users, 'user': user, 'tag':tag, 'tagUsers':tagUsers  }
        return render(request, self.template_name, context)

【问题讨论】:

    标签: python django python-3.x django-templates django-views


    【解决方案1】:

    存在多个问题。让我们从这个开始:

     <a href="{% url 'profile_with_pk' pk=user.username %}">
         {{ post.post }}
     </a>
    

    整个帖子都包含在链接中。我想你想链接到帖子作者,对吧?然后(只猜测你没有发布的模型模式),你需要这样的东西:

    # iteration through posts somewhere here
    <a href="{% url 'profile_with_pk' pk=user.username %}">user.username</a>
    {{ post.post }}
    

    接下来,如果您标记了用户,您需要以某种方式将它们放入帖子的模板中。根据您希望如何将链接指向提及的用户,这可能类似于:

     # iteration through posts somewhere here
    <a href="{% url 'profile_with_pk' pk=user.username %}">user.username</a>
    {{ post.post }}
    Mentions: {% for tagged_user in tagUsers %} {{ tagged_user.username }} {% endfor %}
    

    或者您想直接在帖子文本中插入链接?然后你需要在将它传递给模板之前构建一个帖子结构(示例被剪断;需要改进):

    from django.utils.html import mark_safe
    
    def post(self, request, pk=None):
        ...
        post_text = post.post
        tagUsers = re.findall(r"@+(\w+)", post.post)
        tagged_users = list()
        for username in tagUsers: 
            user = User.objects.get(username=username) 
            for match in re.finditer(username, post_text):
                start = match.start()
                end = match.end()
                profile_link = reverse('profile_with_pk', kwargs={'pk': user.id})
                post_text[start:end] = mark_safe(f'<a href="{profile_link}">username</a>')
        ...
        # return post_text to template context and use in the same way as used post
    

    【讨论】:

    • 你帮了这么多忙,我可以给你买杯咖啡什么的吗?非常感谢
    • 不,真的是你的天使,我将尝试其中一些,看看是否能得到想要的结果,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多