Django templatetags在模板中呈现为None

时间:2017-01-07 16:41:34

标签: python django django-templates templatetags

有人可以帮我解决这个问题,为什么我的模板标签没有在模板中呈现?但是,只改为None

以前我使用Django==1.10.4

demo

1。 templatetags/total_tags.py,我已在此文件夹中创建了__init__.py

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

  

{% total_topics %}的模板标签与{% 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>
  

另一个想法,我尝试了这个答案:https://stackoverflow.com/a/12143011。并使用@register.assignment_tag处理它,但仍然效果不佳,而只是None

0 个答案:

没有答案