【发布时间】:2016-11-25 09:53:23
【问题描述】:
我有一个包含一些行的 models.py 文件,我想在我的 HTML 模板上返回与我的过滤器 QuerySet 对应的所有行。
#models.py
def Test(request) :
CarCollection = MyModel.objects.filter(car="old")
context = {
"CarCollection" : CarCollection
}
return render(request, 'car.html', context)
我的 html 模板看起来像:
<!-- car.html -->
{% block content %}
<ul>
{% for car in CarCollection %}
<li>{{ car }}</li>
{% endfor %}
</ul>
{% endblock %}
但我的对象看起来像:
Volvo_old_car
Audi_old_car
Nissan_new_car
old_Bentley
所以我想在我的对象中隔离一个字符串(例如old)并返回所有带有这个字符串的对象。但是这个字符串可以在开头、中间或结尾。
过滤器将返回:
Volvo_old_car
Audi_old_car
old_bentley
我需要使用正则表达式来做到这一点吗?
提前谢谢你
【问题讨论】:
标签: python django django-queryset