我想在模板中显示女子联赛,这是我的views.py代码:leagues = League.objects.all()for league in leagues: print(league.name)它将向我显示所有联赛的名称:International Conference of Amateur Ice HockeyInternational Collegiate Baseball ConferenceAtlantic Federation of Amateur Baseball PlayersAtlantic Federation of Basketball AthleticsAtlantic Soccer ConferenceInternational Association of Womens' Basketball PlayersAmerican Conference of Amateur FootballAtlantic Amateur Field Hockey LeagueTransamerican Womens' Football Athletics ConferencePacific Ice Hockey Conference我如何循环遍历这些名称并挑选出包含女子联赛的联赛?
2 回答
达令说
TA贡献1821条经验 获得超6个赞
简单,使用contains
或icontains
因为不区分大小写的搜索:
leagues = League.objects.filter(name__icontains='women')
for league in leagues:
print(league.name)
冉冉说
TA贡献1877条经验 获得超1个赞
您可以league.name在打印之前检查小写字母是否包含“women”:
leagues = League.objects.all()
for league in leagues:
if "women" in league.name.lower():
print (league.name)
- 2 回答
- 0 关注
- 105 浏览
添加回答
举报
0/150
提交
取消