Django templatetags rendered as None in the template

315 views Asked by At

Somebody can help me how to solve this problem, why my templatetags isn't rendered in the template? but, only rendered as None instead.

Previously I working with Django==1.10.4.

demo

1. templatetags/total_tags.py, I already created __init__.py inside this folder.

from django import template
from myapp.models import Category

register = template.Library()

@register.simple_tag
def total_categories():
    """
    {% load total_tags %}
    {% total_categories %}

    used in: `includes/menus_dashboard.html`
    """
    print(Category.objects.all())   # this worked well
    Category.objects.all().count()

2. myapp/dashboard.html

{% extends "base.html" %}
{% load i18n %}
{% block title %}{% trans "Dashboard" %} :: {{ block.super }}{% endblock %}

{% block content %}
  <div class="ui two column stackable grid">
    <div class="four wide column dashboard-menu">
      {% include "includes/menus_dashboard.html" %}
    </div>
  </div>
{% endblock %}

3. includes/menus_dashboard.html

The templatetags of {% total_topics %} is similiar with {% total_categories %}

{% load total_tags %}

<div class="ui fluid large inverted vertical pointing menu">
  <a class="active item">
    Dashboard
  </a>
  <a class="item">
    Categories <div class="ui small label">{% total_categories %}</div>
  </a>
  <a class="item">
    Topics <div class="ui small label">{% total_topics %}</div>
  </a>
  <a class="item">
    Moderators <div class="ui small label">2</div>
  </a>
  <div class="item">
    <div class="ui icon input">
      <input type="text" placeholder="Search threads...">
      <i class="search icon"></i>
    </div>
  </div>
</div>

Another idea, I tried like this answer: https://stackoverflow.com/a/12143011. and handle it with @register.assignment_tag, but still doesn't work well and only None instead.

0

There are 0 answers