如何在Django模板中显示经过过滤的对象的其他条目

时间:2022-08-29 20:22:24



In my Django-Project I have a model, that looks like that:

在我的django项目中,我有一个模型,看起来是这样的:

class Tag_car(models.Model):
    car = models.ForeignKey(Car)
    tag = models.ForeignKey(Tag)

car is a foreign key of an ImageField and tag is a foreign key of a TextField.

car是ImageField的外键,tag是TextField的外键。

Every car has more than only one tag. Now users should be able to search for a specific car by a tag. I am doing it like that in my view:

每辆车都有不止一个标签。现在用户应该能够通过标签搜索特定的汽车。在我看来,我是这样做的:

…
search = request.POST.get('search')
cars = Tag_car.objects.filter(tag=search)
…
return render_to_response(page,context_instance=RequestContext(request, {'cars': cars}))

And in my Template I am displaying all cars then like that:

在我的模板中,我展示所有的车,然后像这样:

{% for car in cars %}
    <img src="{{ MEDIA_URL }}{{ car.car }}">
{% endfor %}

It all works fine.
But now I also would like to display within the for-loop in the template all tags that are related to each car. When I do something like {{ car.tag }} I get obviously only the one tag for which the user was searching.
I am struggling how to pass all tags for each car to the view and then display them for each car.
Does anybody have an idea how to do that?
I appreciate your help.

一切都没问题。但是现在我还想在模板的for循环中显示与每辆车相关的所有标记。当我做类似{car}的事情时。很明显,我只得到了用户搜索的一个标签。我正在努力如何将每一辆车的标签传递给视图,然后为每辆车显示它们。有人知道怎么做吗?我很感激你的帮助。

2 个解决方案

#1


1  

So, the issue is that the object you're passing to the template as cars is actually a list of Tag_car objects. So, in order to get the list of other tags related to each car, you need to follow the relationship to the car, then query the other tags:

问题是,作为cars传递给模板的对象实际上是Tag_car对象的列表。因此,为了获得与每辆车相关的其他标签列表,您需要跟踪与汽车的关系,然后查询其他标签:

{% for car in cars %}
    ...
    {% for tag in car.car.tag_set.all %}
        ...
    {% endif %}
{% endif %}

Note though that you should probably query Car objects in the first place, rather than Tag_cars. Plus, your Tag_car model is really just the through table of a many-to-many relationship, so rather than creating it explicitly you can just use a M2M field on the Car model:

请注意,您应该首先查询Car对象,而不是Tag_cars。另外,您的Tag_car模型实际上是多对多关系的传递表,所以您可以在Car模型上使用M2M字段,而不是显式地创建它:

tags = models.ManyToManyField(Tag)

#2


0  

Iterate over car.tag.all in the template:

遍历car.tag。所有的模板:

{% for car in cars %}
    <img src="{{ MEDIA_URL }}{{ car.car }}">

    <ul>
    {% for tag in car.tag.all %}
        <li>{{ tag }}</li>
    {% endfor %}
    </ul>
{% endfor %}

Also see: Traversing foreign key related tables in django templates

还请参见:在django模板中遍历外键相关的表

#1


1  

So, the issue is that the object you're passing to the template as cars is actually a list of Tag_car objects. So, in order to get the list of other tags related to each car, you need to follow the relationship to the car, then query the other tags:

问题是,作为cars传递给模板的对象实际上是Tag_car对象的列表。因此,为了获得与每辆车相关的其他标签列表,您需要跟踪与汽车的关系,然后查询其他标签:

{% for car in cars %}
    ...
    {% for tag in car.car.tag_set.all %}
        ...
    {% endif %}
{% endif %}

Note though that you should probably query Car objects in the first place, rather than Tag_cars. Plus, your Tag_car model is really just the through table of a many-to-many relationship, so rather than creating it explicitly you can just use a M2M field on the Car model:

请注意,您应该首先查询Car对象,而不是Tag_cars。另外,您的Tag_car模型实际上是多对多关系的传递表,所以您可以在Car模型上使用M2M字段,而不是显式地创建它:

tags = models.ManyToManyField(Tag)

#2


0  

Iterate over car.tag.all in the template:

遍历car.tag。所有的模板:

{% for car in cars %}
    <img src="{{ MEDIA_URL }}{{ car.car }}">

    <ul>
    {% for tag in car.tag.all %}
        <li>{{ tag }}</li>
    {% endfor %}
    </ul>
{% endfor %}

Also see: Traversing foreign key related tables in django templates

还请参见:在django模板中遍历外键相关的表