从数据库中检索Django用户关注者

2024-04-20 05:56:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用户跟随系统的应用程序,我已经能够实现,但我试图检索个人用户追随者和跟随,但我无法得到它。下面是我的代码

视图.py

def following(request):
    query = Contact.objects.filter(request.user.following)
    context = {
        'query': query
        }
    template = 'following.html'
    return render(request, template, context)

模型.py

^{pr2}$

模板

{% load staticfiles %}
{% block content %}
<h2>following</h2>
<div id="action-list">

<h1>{{ results.get_full_name }}</h1>
</div>
{% endblock %}

如有要求,将添加附加代码。在


Tags: 代码用户pydiv应用程序request系统context
1条回答
网友
1楼 · 发布于 2024-04-20 05:56:49

这可以通过两种方式解决。第一:过滤器不正确:

def following(request):
    query = Contact.objects.filter(user_from=request.user)
    context = {
        'query': query
        }
    template = 'following.html'
    return render(request, template, context)

使用related_name的反向关系的另一个解决方案

^{pr2}$

当然,对于Contactuser_to字段的工作原理类似。在

编辑:您的模板没有使用您在视图following中定义的正确上下文变量。所以你需要稍微调整一下:

^{3}$

相关问题 更多 >