我无法获得显示的inclusion_tag的内容。我没有收到错误,所以我知道标签正在注册,我几乎可以肯定它正在正确加载。标签在crudapp/templatetags/crudapp_tags.py创建Django inclusion_tag内容不显示
from django import template
register = template.Library()
@register.inclusion_tag("forum.html")
def results(poll):
form = 'blah'
return {'form': form}
模板/ forum.html
{% extends 'index.html' %}
{% load crudapp_tags %}
{% results poll %}
<p>aaa</p>
{% block homepage %}
<p>bbb</p> <!-- Only this displays -->
{% if form %}
<p>Form exists</p>
{% endif %}
{% for item in form %}
<p>This is {{ item }}</p>
{% endfor %}
<div>
<p>{% if user.is_authenticated %}Add a New Topic: <a href="{% url 'topic_form' %}"><span class="glyphicon glyphicon-plus"></span></a>{% endif %}</p>
</div>
<div>
<p>{{ totalposts.count }} posts, {{ totaltopics.count }} topics, {{ totalusers.count }} users, {{ totalviews.numviews}} views</p>
</div>
{% endblock %}
设置文件如下,
这里的东西没有意义;您的包含标签正在呈现使用标签本身的模板。 –
另外'templates'目录应该位于你的app目录中,而不是项目的根目录,除非你已经明确地告诉Django去看那里。 – solarissmoke
我认为你误解了包含标签的作用。包含标签呈现*另一个*模板。由于您在块外部有'{%results poll%}',因此标记的结果将永远不会显示。也许你想要一个[作业标签](https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/#assignment-tags)代替(在Django 1.9中,你可以使用简单的标签而不是分配标签)。 – Alasdair