Can't render on base.html?

56 views Asked by At

I have a base.html which essentially provides the navigation for my app.

I want to be able to add some stats into the nav bar - so I am trying to render them onto base.html

The same script works, if I change base.html to home.html, but I don't want to repeat the same code on every page, to retain the stats when I navigate away from home.

Here is my view:

def stats(request):
    incompletetasks = Todo.objects.filter(complete=False, creator=request.user).count()
    shared = Todo.objects.filter(Q(shareslotone=request.user) | Q(shareslottwo=request.user) | Q(shareslotthree=request.user) | Q(shareslotfour=request.user) | Q(shareslotfive=request.user)).count()
    closedtasks = Todo.objects.filter(complete=True, creator=request.user).count()
    context = {'incompletetasks':incompletetasks, 'shared':shared, 'closedtasks':closedtasks}
    return render(request, 'base.html', context)

How can I make this render to base.html, rather than copying the same code for every page?

Thanks

1

There are 1 answers

0
Clifton Avil D'Souza On BEST ANSWER

You can use template tag for it

filters.py

register = template.Library()

 @register.filter(name="get_model_count")
 def get_model_count(model, app_label):
return apps.get_model(model_name=model, app_label=app_label).objects.count()

your_file.html

{% load filters %} #load it at the top of html file

<h2>{{ "your_model_name"|get_model_count:"your_app_name" }}</h2> #use this tag where you want to have count

Note:This if for only one count modifiy as per requirements