【发布时间】:2021-11-03 19:58:00
【问题描述】:
我有一个 Django 自定义模板标签,它根据输入的值改变字体的颜色。
一切正常,但我想知道是否有一种更简洁的方式来编写此代码,而不是编写所有这些 elif 语句。显示的球队甚至不到一半,还有 3 个其他联赛的球队会被包含在该标签中。
@register.filter(name='teamColor')
def teamColor(team):
if team == "Celtics" or team == "Jazz":
return "green"
elif (team == "Hawks" or team == "Bulls" or team == "Rockets"
or team == "Pistons" or team == "Clippers" or team == "Heat" or
team == "Trail Blazers" or team == "Raptors"):
return "red"
elif team == "Nets":
return "grey"
elif (team == "Hornets" or team == "Lakers" or team == "Suns" or
team == "Kings"):
return "purple"
elif team == "Cavaliers":
return "#b11226"
elif team == "Mavericks" or team == "Grizzlies" or team == "76ers":
return "blue"
elif team == "Nuggets" or team == "Pacers":
return "yellow"
elif (team == "Warriors" or team == "Timberwolves" or team == "Thunder" or
team == "Knicks"):
return "#1D428A"
elif team == "Bucks":
return "#00471b"
elif team == "Pelicans":
return "gold"
elif team == "Magic":
return "#0077C0"
elif team == "Spurs" or team == "Wizards":
return "silver"
【问题讨论】:
标签: python django templatetags