Problems with simple_tag array values in Django Template

大憨熊 提交于 2019-12-19 04:58:34

问题


I'm trying to pass an array to my template with a simple tag. I created my module under app/templatetags/pages_navigation.py and in my opinion the code should be alright:

from django import template
from pages.models import Page

register = template.Library()

@register.simple_tag(name='links')
def pages_navigation():
  pages = Page.objects.all()
  links = [['Events','/']]

  for page in pages:
    links.append([page.title, '/'+page.url])

  return {'links':links}

In my template I try to access links like this:

<ul>
{% if links %}
  {% for link in links %}
    <a href="{{link.1}}"><li>{{link.0}}</li></a>
  {% endfor %}
{% else %}
  <li>no pages found</li>
{% endif%}
</ul>

However, somehow it seems like links is always empty. When I tried the pages_navigation method in the python shell it worked fine..

Is it possible that you can't return arrays from simple tag methods?


回答1:


From the docs:

Many template tags take a number of arguments -- strings or template variables -- and return a page_navigation after doing some processing

simple tags are for printing a piece of information, not assigning some result to a variable (a list in your case)

So, you would be better of using an inclusion tag:

@register.inclusion_tag('links.html')
def page_navigation(a, b, *args, **kwargs):
    pages = Page.objects.all()
    links = [['Events','/']]
    for page in pages:
        links.append([page.title, '/'+page.url])
    return {'links':links}

and creating a links.html file in your template directory:

<ul>
{% if links %}
    {% for link in links %}
    <a href="{{link.1}}"><li>{{link.0}}</li></a>
    {% endfor %}
{% else %}
    <li>no pages found</li>
{% endif%}
</ul>

and in your original template you can include this:

{% load pages_navigation %}
{% page_navigation %}

which will call the template tag, render it and insert it into your template




回答2:


In new Django 1.4, you could use Assignment Tags https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags-assignment-tags

from django import template

register = template.Library()

@register.assignment_tag
def get_links():
    return (('Events', '/'),) + tuple((p.title, '/'+p.url) for p in Page.objects.all())

{# in template #}
{% get_links as links %}

You could port it to your Django version if you like it.

Furthermore, if your page URLs is like /?page=2, you could try django-pagination http://pypi.python.org/pypi/django-pagination



来源:https://stackoverflow.com/questions/9860717/problems-with-simple-tag-array-values-in-django-template

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