1

I'm trying to dynamically set the bootstrap "class=" variable in Django to a value from the model.

The template looks like this (when "statically" set):

<strong><span class="label label-success"> It Worked! </span></strong>

I would like to dynamically set the "class =" and I have tried follow the following but seem to have issues passing {{challenge.labelbx}} into the span tag :

<strong><span class={{challenge.labelbx}}> It Worked! </span></strong>

From the Challenge Model:

def labelbx(self): #These are bootstrap label colors
    if self.consistency_percentage() > 79:
        return "label label-success" # green
    elif self.consistency_percentage() > 69:
        return "label label-info" #blue
    elif self.consistency_percentage() > 59:
        return 'label label-warning' #orange
    elif self.consistency_percentage() > 49:
        return "label"
    else:
        return "label"

From the View:

def public_scorecard_page(request):

    c = Challenge.objects.get(user__exact = request.user,chal_status=1)

    return render(request, 'public_scorecard.html', {'challenge': c})
4

0 回答 0