找到你要的答案

Q:django change something in template based on foreign key

Q:Django改变基于外键模板中的东西

I know it's very basic but I can't figure out how to resolve my problem.
I have a model like this:

class Follow(models.Model):
    followed = models.ForeignKey(User, related_name='followed')
    follower = models.ForeignKey(User, related_name='followers')

my view:

def usersList(request):
    users = User.objects.all()
    return render(request, 'blog/membres.html', {'users': users})

And here is what I want to do in my template:

{% for user in users %}
    {% if request.user in user.followers.all %}
        I'm following you !
    {% else %}
        I'm not following you
    {% endif %}
{% endfor %}

But it's not working. I think I have to do something like this:

{% for user in users %}
    {% if user.followers.filter(follower=request.user) %}
        I'm following you !
    {% else %}
        I'm not following you
    {% endif %}
{% endfor %}

I know it's very basic but I can't figure out how to resolve my problem.
I have a model like this:

class Follow(models.Model):
    followed = models.ForeignKey(User, related_name='followed')
    follower = models.ForeignKey(User, related_name='followers')

我的观点:

def usersList(request):
    users = User.objects.all()
    return render(request, 'blog/membres.html', {'users': users})

这里是我想做的我的模板:

{% for user in users %}
    {% if request.user in user.followers.all %}
        I'm following you !
    {% else %}
        I'm not following you
    {% endif %}
{% endfor %}

但它不工作。我想我必须这样做:

{% for user in users %}
    {% if user.followers.filter(follower=request.user) %}
        I'm following you !
    {% else %}
        I'm not following you
    {% endif %}
{% endfor %}
answer1: 回答1:

You have to pass the users object to template correctly...

def home(request):
    users = User.objects.all()
    return render(request, 'pages/test.html', {'users':users})

And HTML logic is correct it will work perfectly...

必须将用户对象正确传递给模板…

def home(request):
    users = User.objects.all()
    return render(request, 'pages/test.html', {'users':users})

与HTML的逻辑是正确的它将完美的工作…

answer2: 回答2:

So I found a way to bypass my problem.

I add this method to my follow model:

def getListFollowed(self, user):
    relations = Follow.objects.filter(follower=user)
    liste = []
    for relation in relations:
        liste.append(relation.followed.username)
    return liste

I change my view this way:

def membres(request):
    users = User.objects.all()
    f = Follow()
    relations = f.getListFollowed(request.user)
    return render(request, 'blog/membres.html', locals())

And i did this in my template:

{% if user in relations %}
    I'm not following you!
{% else %}
    I'm following you !
{% endif %}

I hope in can help some other who can have the same problem as me

所以我找到了解决问题的方法。

我把这个方法添加到我的跟随模型中:

def getListFollowed(self, user):
    relations = Follow.objects.filter(follower=user)
    liste = []
    for relation in relations:
        liste.append(relation.followed.username)
    return liste

我改变我的看法:

def membres(request):
    users = User.objects.all()
    f = Follow()
    relations = f.getListFollowed(request.user)
    return render(request, 'blog/membres.html', locals())

我在我的模板:

{% if user in relations %}
    I'm not following you!
{% else %}
    I'm following you !
{% endif %}

我希望在能帮助其他一些谁可以有同样的问题,因为我

python  django  templates  foreign-keys