Problems with simple_tag array values in Django Template

前端 未结 2 1650
别跟我提以往
别跟我提以往 2021-01-07 15:01

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:

2条回答
  •  灰色年华
    2021-01-07 15:45

    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

提交回复
热议问题