Álvaro Álvaro - 4 months ago 9x
Python Question

Using Django numeric variable in css

I'm trying to get a bootstrap progress bar to work, but I'm running into a problem. I want to get the progress bar width from a Django variable, I use the templates language for this. The problem is, HTML changes the float variable (i.e 32.54) to a string (i.e 32,54). I'm not sure why this happens (my guess is encoding) but the width attribute will not work if the variable is not a number.

To clarify, this is my code.


def details(request):
estudio = request.GET.get('estudio', '')
uni = request.GET.get('uni', '')
campus = request.GET.get('campus', '')
result = Titulaciones.objects.raw('SELECT * FROM tasas t INNER JOIN titulaciones tit on t.codigo_titulacion = tit.codigo_titulacion INNER JOIN impartida_en imp ON tit.codigo_titulacion = imp.codigo_titulacion INNER JOIN centros cent ON imp.codigo_centro = cent.codigo_centro WHERE cent.universidad = %s and tit.nombre = %s and cent.campus =%s', [uni, estudio, campus]);
return render(request, 'proyecto_uni/details.html', {'result':result})

(If I print here, the variable still shows as a number).


{% extends "base.html" %}

{% block content %}

{% for foo in result %}
<ul class="nav nav-tabs">
<li role="presentation"><a href="#tab1" >General</a></li>
<li role="presentation"><a href="#tab2">Asignaturas</a></li>
<li role="presentation"><a href="#tab3">Resultados</a></li>

<!-- TAB 1 -->
<div id="tab1">
<div class="panel panel-primary">
<!-- Default panel contents -->
<div class="panel-heading">{{foo.nombre}}</div>
<div class="panel-body">
<p>{{ foo.universidad }}</p>
<hr class="m-y-2">
<p>{{ foo.campus }}</p>
<hr class="m-y-2">
<p>Aquí debería ir la descripción</p>
<hr class="m-y-2">
<h4>Nota de corte:</h4>
<p>{{ foo.nota_corte }}</p>

<!-- TAB 2 -->
<div id="tab2">

<!-- TAB 3 -->
<div id="tab3">
<div class="panel panel-primary">
<div class="panel-heading">{{foo.nombre}}</div>
<div class="panel-body">
<div class="progress">
<div class="progress-bar progress-bar-success" style="width: {{foo.rendimiento}}%">
<span class="sr-only">{{foo.rendimiento}}% Complete (success)</span>
<!-- The problem is right above, in the width style tag -->
<div class="progress-bar progress-bar-warning" style="width: 2%">
<span class="sr-only">22% Complete (warning)</span>
<div class="progress-bar progress-bar-danger" style="width: 1%">
<span class="sr-only">1% Complete (danger)</span>

{% endfor %}
{% endblock%}

I posted all the code to make it easier to understand, hope I didn't make it more confusing. Anyways, thanks in advance!


Django formats numbers according to your localization settings. You can turn localization off by using:

{% load l10n %}
{% localize off %}
{% endlocalize %}

You can also unlocalize one single value:

{% load l10n %}

Django format localization documentation