In my views.py I have this code
from .forms import *
from students.models import Student
from classes.models import Class
from venues.models import Venue
from courses.models import Course
from registrations.models import Registration
class Test(View):
template_name = "test.html"
context = {}
data_summary = {
"total_students": Student.objects.all().count(),
"total_classes": Class.objects.all().count(),
"total_courses": Course.objects.all().count(),
"total_registrations": Registration.objects.all().count(),
}
def get(self,*args, **kwargs):
return render(self.request,self.template_name,self.data_summary)
In my test.html I have this:
<...snip ...>
<h3> Totals: </h3>
<hr>
</div>
<div class="row">
<div class="col-md-2 text-right">
<label style="color: Blue; font-size:24"> Students: </label>
</div>
<div class="col-md-2 text-right">
{% if total_students %}
<... snip ...>
The template renders very nicely but if I update my database and add another student and or class and reload my page the data in my dictionary does not update.
I am at a loss why the data doesn't update. I am now banging my head and not in the 70's good way either.
In your case, the data_summary
is a class attribute, and is created when the Test
class is declared the first time (on module load).
you can move it to the get
method, and be assured that the database calls happen whenever the page get
happens
def get(self,*args, **kwargs):
data_summary = {
"total_students": Student.objects.all().count(),
"total_classes": Class.objects.all().count(),
"total_courses": Course.objects.all().count(),
"total_registrations": Registration.objects.all().count(),
}
return render(self.request,self.template_name,data_summary)