I need to use if else condition for Value1 in my queryset..I have found average for my records in the queryset..I need to apply condition like..If my Value1 is between 90 and 110 then I have to get message as “great” and if its below 90 it must print as “low” and if above 140 it must print as “high”
I need to be print this output on my html file
Models.py:
from django.db import models from django.contrib.auth.models import User class graphinput(models.Model): user=models.ForeignKey(User,on_delete=models.CASCADE) Month = models.CharField(max_length=30) Value1 = models.IntegerField() Value2 = models.IntegerField()
urls.py:
def average(request): userid = None if request.user.is_authenticated: userid = request.user.id print(userid) dataset = graphinput.objects.filter(user=userid) .values('Value1') a=dataset.aggregate(Sum('Value1')) b=dataset.aggregate(Avg('Value1')) print(a) print(b) return render(request, 'profile.html')
Advertisement
Answer
avg = b.get('Value1__avg', 0) if avg < 90: message = 'low' else if avg < 110: message = 'great' else: message = 'high' context = {'message': message} return render(request, 'profile.html', context)
next access it in template with {{ message }}