Django template - reading objects through inclusion tag

六月ゝ 毕业季﹏ 提交于 2019-12-25 08:18:20

问题


Continuing from here, I'm trying to create a global nav using values in a model for the link in the nav. I've been pointed in the right direction with inclusion tags, but I'm still not able to work with the objects my inclusion tag returns...I just get blank. I have a simple model called Division:

class Division(models.Model):
    DivisionAbbrev = models.CharField(max_length=20)

I have included the template navigation.html in base.html. I am trying to use navigation.html to show the different DivisionAbbrev, using an inclusion_tag. In current_tags.py I create a dictionary of each object in Division:

current_tags.py

from django import template
from libs.display.models import Division
register = template.Library()
@register.inclusion_tag('display/navigation.html')
def get_navigation(self=None):
        return {
                'navigation': Division.objects.all(),
        }

Then in navigation.html:

{% load current_tags %}
<p>Test</p> # This prints, so I know navigation.html is included correctly
{% for item in get_navigation %} # This doesn't print anything
<p>{{ item.DivisionAbbrev }}</p>
{% endfor %}

base.html

{% include 'display/navigation.html' %}

来源:https://stackoverflow.com/questions/22595237/django-template-reading-objects-through-inclusion-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!