Django Model Choice Form display logo + name

36 views Asked by At

How to make a form populated from a database values to display the image from the URLField (team.logo) and the name (team.name) next to it.

I am trying to achieve this through the following code but it ignores the image display part and only shows the team name:

class TeamModelChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, team):
        return format_html(f'<img src="{team.logo}" class="form-logo">{team.name}')


class TeamChoiceForm(forms.Form):
    teams_extra = Team.objects.all()

    team_home = TeamModelChoiceField(
        queryset=teams_extra,
        empty_label="Team Home"
    )

    team_away = TeamModelChoiceField(
        queryset=teams_extra,
        empty_label="Team Away"
    )
0

There are 0 answers