Django Built-In Template Tags in Views

Django’s built-in template tags are simply Python functions which means they can be used in places other than templates such as class-based views.

Here’s an example using pluralize:

from django.views.generic import ListView
from django.template.defaultfilters import pluralize
from myapp.models.Product
class MyView(ListView):
queryset = Product.objects.all()
template_name = 'product.html'
context_object_name = 'products'
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
context['title'] = len(context['products']) + ' Product' + pluralize(context['products']) + ' Found'
return context

view raw
view.py
hosted with ❤ by GitHub

One thought on “Django Built-In Template Tags in Views”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s