Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:09:31+00:00 2026-06-02T18:09:31+00:00

I need to implement a template tag that will return a string with a

  • 0

I need to implement a template tag that will return a string with a collection of items from an object.

I had created the following structure:

produtos/        
    templatetags/
        __init__.py
        produto_tags.py   

produto_tags.py:

# -*- coding: utf-8 -*-

from django import template
from django.template import Node
from produto.models import Produto
from django.template.loader import render_to_string

register = template.Library()

@register.tag
def get_all_tags(parser, token):
    args = token.split_contents()
    return ProdutoTemplateNode(args[1])


class ProdutoTemplateNode(Node):
    def __init__(self, produto):
        self.produto = produto

    def render(self, context):
        list = []
        produto = template.Variable(self.produto).resolve(context)
        tags = produto.tags.all()
        if tags:
            for tag in tags:
                list.append(tag.name)
            return ", ".join(list)
        else:
            return u'Não existem tags para este produto'

Template:

{% load produto_tags %}
...
    {% for produto in produtos %}
        <li id="{{ produto.ordenacao }}" data-tags="{% get_all_tags produto %}">
            ...
        </li>
    {% endfor %}
    </ul>
{% else %}
    <p>Não existem produtos cadastrados no sistema</p>
{% endif %} 

I am receiving this error:

TemplateSyntaxError at /concrete/nossos-sites.html
Invalid block tag: 'get_all_tags', expected 'empty' or 'endfor'

I read other threads where people said this error occurs if the Tag does not exist and it seems to be the case. I’ve been looking on the djangoproject.com documentation as well and I could not find any clue about what might be happening.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:09:32+00:00Added an answer on June 2, 2026 at 6:09 pm

    That was tricky, even though simple:

    First, there was another ‘produto_tags.py’ in another folder elsewhere in the project:

    project/
        common/
            templatetags/
                produtos_tags.py
        produtos/
            templatetags/
                produtos_tags.py
    

    So, at first I have moved all code from produtos/templatetags/ to common/templatetags/. But when I did it Django started whining about not finding the produtos_tags from produtos. Afterwards I got the code back to produtos/templatetags/ and renamed the file to tags_produtos.py, what had worked to show the easy part that is my wrong import below:

    Wrong:

    from produto.models import Produto
    

    Correct:

    from produtos.models import Produto
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement a custom handler for MVC that gives me the first
I need to implement handling of redelivery of JMS messages in the application that
I need to implement something. Something that could do some certain task in my
I need to implement a functor that takes any (!) function pointer when instantiated,
Suppose I need to implement factory function which returns object O which inherits/has members
Is it possible to implement a class template in such a way that one
While writing several math utilities I bumped into need to implement generic utility that
I need to implement a custom error page in my rails application that allows
I need to compare the performances of a Flask application that uses jinja2.Template.render against
I have a project (Web Application) that I need to implement with Web Forms.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.