[Answered ]-Django not showing updated data from database

2👍

It was really easy. The answer is in the documentation here and here.

Therefore, changing the MemberDirectory listview as shown below actually worked !

class MemberDirectory(LoginRequiredMixin, ListView, FormView):

    model = Member
    template_name = 'members/directory.html'
    paginate_by = 5

    try:
        context = {
            'total_members': Member.objects.all().count(),
            'active_members':     Member.objects.filter(is_active=True).count(),
            'staff_members': Member.objects.filter(is_staff=True).count(),
        }
    except Exception:
        context = {}

    def get(self, request, *args, **kwargs):
        self.context['members_list'] = Member.objects.all().order_by('-date_joined')
        return render(request, self.template_name, self.context)

    def post(self, request, *args, **kwargs):

        member_number = int(self.request.POST['member_number'])

        if not member_number or not isinstance(member_number, int):
            messages.error(request, 'errors occured ...', 'alert alert-danger')
            return render(request, self.template_name, self.context)

        self.context['members_list'] = Member.objects.filter(member_number__exact=member_number)
        return render(request, self.template_name, self.context)

Simply put, the previous listview was using a queryset that contained cached data before changes were made to the database.

Leave a comment