Tak jak w temacie potrzebuje zoptymalizować mój kód
def home(request):
queryset = Mecz.objects.order_by('-widownia')
pogon = Mecz.objects.filter(stadion_nr=2).aggregate(Avg('widownia'))
pogon_max = Mecz.objects.filter(stadion_nr=2).aggregate(Max('widownia'))
pogon_min = Mecz.objects.filter(stadion_nr=2).aggregate(Min('widownia'))
pogon_sum = Mecz.objects.filter(stadion_nr=2).aggregate(Sum('widownia'))
legia = Mecz.objects.filter(stadion_nr=1).aggregate(Avg('widownia'))
legia_max = Mecz.objects.filter(stadion_nr=1).aggregate(Max('widownia'))
legia_min = Mecz.objects.filter(stadion_nr=1).aggregate(Min('widownia'))
legia_sum = Mecz.objects.filter(stadion_nr=1).aggregate(Sum('widownia'))
lech = Mecz.objects.filter(stadion_nr=5).aggregate(Avg('widownia'))
lech_max = Mecz.objects.filter(stadion_nr=5).aggregate(Max('widownia'))
lech_min = Mecz.objects.filter(stadion_nr=5).aggregate(Min('widownia'))
lech_sum = Mecz.objects.filter(stadion_nr=5).aggregate(Sum('widownia'))
lechia = Mecz.objects.filter(stadion_nr=8).aggregate(Avg('widownia'))
lechia_max = Mecz.objects.filter(stadion_nr=8).aggregate(Max('widownia'))
lechia_min = Mecz.objects.filter(stadion_nr=8).aggregate(Min('widownia'))
lechia_sum = Mecz.objects.filter(stadion_nr=8).aggregate(Sum('widownia'))
klub_pogon = Klub.objects.filter(Nazwa='Pogon Szczecin')
klub_legia = Klub.objects.filter(Nazwa='Legia Warszawa')
klub_lech = Klub.objects.filter(Nazwa='Lech Poznań')
klub_lechia = Klub.objects.filter(Nazwa='Lechia Gdańsk')
context = {
"pogon":pogon,
"pogon_max":pogon_max,
"pogon_min":pogon_min,
"pogon_sum":pogon_sum,
"legia":legia,
"legia_max":legia_max,
"legia_min":legia_min,
"legia_sum":legia_sum,
"lech":lech,
"lech_max":lech_max,
"lech_min":lech_min,
"lech_sum":lech_sum,
"lechia":lechia,
"lechia_max":lechia_max,
"lechia_min":lechia_min,
"lechia_sum":lechia_sum,
"queryset":queryset,
"klub_pogon":klub_pogon,
"klub_legia":klub_legia,
"klub_lech":klub_lech,
"klub_lechia":klub_lechia
}
return render(request, "home.html", context)
chciałbym, żeby kod się nie powtarzał i był dynamiczny - cos typu (stadion_nr=stadion_nr lub (stadion_nr=id ).
Wiem jak to zrobić gdy mam 1 wartość do wyświetlenia na raz na jednej stronie, ale tu w tym przypadku jest kilka klubów.
Nie wiem czy sie przyda ale dodam models.py i htmla jeszcze
class Stadion(models.Model):
nazwa = models.CharField(max_length=50, blank=False, null=False, unique=True)
miasto = models.CharField(max_length=50,blank=False,null=False,)
ulica = models.CharField(max_length=50,blank=False,null=False,)
pojemnosc = models.IntegerField(blank=False,null=False,)
klubs = models.ForeignKey(Clubs)
klub = models.ForeignKey(Klub)
def __unicode__(self):
return self.nazwa.decode().encode('utf-8')
{% extends "base.html" %}
{% load crispy_forms_tags %}
{% load staticfiles %}
{% block jumbotron_content %}
<head>
{% if pogon_max %}
{% if lech_max %}
{% if lechia_max %}
{% if legia_max %}
<script type="text/javascript">
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer",
{
title:{
text: "Najwyższa Frekwencja"
},
animationEnabled: true,
axisY: {
title: "Liczba kibiców"
},
legend: {
verticalAlign: "bottom",
horizontalAlign: "center"
},
theme: "theme3",
data: [
{
type: "column",
showInLegend: false,
legendMarkerColor: "grey",
legendText: "klbuy",
dataPoints: [
{y: {{ lech_max.widownia__max }}, label: "Lech" },
{y: {{ legia_max.widownia__max }}, label: "Legia"},
{y: {{ lechia_max.widownia__max }}, label: "Lechia"},
{y: {{ pogon_max.widownia__max }}, label: "Pogoń"},
]
}
]
});
chart.render();
}
</script>
{% endif %}
{% endif %}
{% endif %}
{% endif %}
<script type="text/javascript" src="{% static 'js/canvasjs.min.js' %}">
</script>
<script src=https://code.jquery.com/jquery-2.2.4.min.js></script>
</head>
<style>
{% block style %}
#shiva
{
width: 100px;
height: 100px;
background: none;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
float:left;
margin:5px;
}
.count
{
line-height: 100px;
color:black;
margin-left:30px;
font-size:20px;
}
#talkbubble {
width: 120px;
height: 80px;
background: red;
position: relative;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
float:left;
margin:20px;
}
#talkbubble:before {
content:"";
position: absolute;
right: 100%;
top: 26px;
width: 0;
height: 0;
border-top: 13px solid transparent;
border-right: 26px solid red;
border-bottom: 13px solid transparent;
}
.linker
{
font-size : 20px;
font-color: black;
}
.text-align-center{
text-align: center;
padding-top: 10px;
}
{% endblock %}
</style>
<body>
<div class="container ">
<div class="jumbotron">
<div id="chartContainer" style="height: 300px; width: 100%;">
</div>
<table class="table table-striped">
<tr>
<td><strong>Drużyna</strong></td>
<td><strong>Średnia</strong></td>
<td><strong>najmniejsza</strong></td>
<td><strong>łaczna</strong></td>
</tr>
{% if klub_pogon %}
{% for instance in klub_pogon %}
<tr><td>
<strong>{{ instance.Nazwa }}</strong>
<img src= "{% static 'img/pogon.png' %}" class="img-responsive" alt="" width="50" height="40">
</td>
{% endfor %}
{% endif %}
{% if pogon %}
<td>
<div id="shiva"><span class="count">{{ pogon.widownia__avg }}</span></div>
</td>
{% endif %}
{% if pogon_min %}
<td>
<div id="shiva"><span class="count">{{ pogon_min.widownia__min }}</span></div>
</td>
{% endif %}
{% if pogon_sum %}
<td>
<div id="shiva"><span class="count">{{ pogon_sum.widownia__sum }}</span></div>
</td>
{% endif %}
{% if klub_legia %}
{% for instance in klub_legia %}
<tr><td>
<strong>{{ instance.Nazwa }}</strong>
<img src= "{% static 'img/legia.png' %}" class="img-responsive" alt="" width="50" height="40">
</td>
{% endfor %}
{% endif %}
{% if legia %}
<td>
<div id="shiva"><span class="count">{{ legia.widownia__avg }}</span></div>
</td>
{% endif %}
{% if legia_min %}
<td>
<div id="shiva"><span class="count">{{ legia_min.widownia__min }}</span></div>
</td>
{% endif %}
{% if legia_sum %}
<td>
<div id="shiva"><span class="count">{{ legia_sum.widownia__sum }}</span></div>
</td>
{% endif %}
{% if klub_lech %}
{% for instance in klub_lech %}
<tr><td>
<strong>{{ instance.Nazwa }}</strong>
<img src= "{% static 'img/lech.png' %}" class="img-responsive" alt="" width="50" height="40">
</td>
{% endfor %}
{% endif %}
{% if lech %}
<td>
<div id="shiva"><span class="count">{{ lech.widownia__avg }}</span></div>
</td>
{% endif %}
{% if lech_min %}
<td>
<div id="shiva"><span class="count">{{ lech_min.widownia__min }}</span></div>
</td>
{% endif %}
{% if lech_sum %}
<td>
<div id="shiva"><span class="count">{{ lech_sum.widownia__sum }}</span></div>
</td>
{% endif %}
{% if klub_lechia %}
{% for instance in klub_lechia %}
<tr><td>
<strong>{{ instance.Nazwa }}</strong>
<img src= "{% static 'img/lechia.png' %}" class="img-responsive" alt="" width="50" height="40">
</td>
{% endfor %}
{% endif %}
{% if lechia %}
<td>
<div id="shiva"><span class="count">{{ lechia.widownia__avg }}</span></div>
</td>
{% endif %}
{% if lechia_min %}
<td>
<div id="shiva"><span class="count">{{ lechia_min.widownia__min }}</span></div>
</td>
{% endif %}
{% if lechia_sum %}
<td>
<div id="shiva"><span class="count">{{ lechia_sum.widownia__sum }}</span></div>
</td>
{% endif %}
</table>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$('.count').each(function () {
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
$(this).text(Math.ceil(now));
}
});
});
</script>
</body>
{% endblock %}