Tuesday, 15 July 2014

django - How to use a custom template tag in base.html -



django - How to use a custom template tag in base.html -

. project root, manage.py resides. have base of operations template @ ./templates/base.html. have custom template tag in ./app/templatetags/mytags.py

from django import template register = template.library() @register.unread_tag def get_unread(user): homecoming user.notification_set.filter(viewed=false).count()

how create tag usable base.html, app-level templates inherit.

your tag definition not correct. need utilize register.simple_tag decorator:

@register.simple_tag(name='unread') def get_unread(user): homecoming user.notification_set.filter(viewed=false).count()

then, need load tag template:

{% load mytags %}

then, can utilize tag in template:

{% unread request.user %}

django django-templates

No comments:

Post a Comment