I have create and application that keeps track of shoots scores. I have a scoring application that works fine. it use the following models Score
from django.conf import settings
from django.db import models
rifle_type_choices = {(“B”, “B”), (“S”, “S”)}
optic_type_choices = {(“O”, “O”), (“S”, “S”)}
range_distance_choices = {(“50”, “50”), (“100”, “100”)}
class Scores(models.Model):
date = models.DateField()
match_score = models.IntegerField()
xcount = models.IntegerField()
rilfe_type = models.CharField(max_length=1, choices = rifle_type_choices, default = “B”)
optic_type = models.CharField(max_length=1, choices = optic_type_choices, default = “O”)
range_distance = models.CharField(max_length=3,choices = range_distance_choices, default = “100” )
username = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
def __str__(self):
return f' {self.username} {self.date}'
I create a App for averaging the score and here is the View
from django.http import HttpResponse
from django.template import loader
from scores.models import Scores
from django.db.models import Avg
def index(request):
template = loader.get_template(“average.html”);
context = {
'avg50': Scores.objects.filter(username=myusername,range_distance="50").aggregate(Avg("match_score")),
'avg100': Scores.objects.filter(username=myusername, range_distance="100").aggregate(Avg("match_score")),
'scores_list': Scores.objects.values('username')
}
return HttpResponse(template.render(context, request))
Here is the average template
{% extends “base.html” %}
{% load static %}
{% block content %}
body {
background-color: lightblue;
text-align: left;
}
table, th, td {
border: 1px solid black;
width: 15em;
table-layout: fixed;
border-collapse: collapse;
text-align: center;
border: black solid 0.1em;
}
</style>
Indviual Average Score "Not working Yet
Username | 50 Yard Avg | 100 Yard Avg |
---|
{%for i in myusername%}
{%endfor%}
{{i.user}} | {{i.avg50}} | {{i.avg100 }} |
{% endblock content %}
I am having a problem figuring out how to loop through the user so that I can figure there average.
any help would be great
thank you
Ed