Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Django: TemplateSyntaxError at /category/2/

1. Summarize the problem

I great custom tag. In file news_tags.py

from django import template

from news.models import Category

register = template.Library()


@register.simple_tag()
def get_categories():
    return Category.objects.all()

I called the tag in the sidebar.html file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{% load news_tags %}
{% get_categories %}

<div class="list-group">
    {% for item in categories %}
    <a href="{% url 'category' item.pk %}" class="list-group-item list-group-item-action">{{ item.title }}</a>
    {% endfor %}
</div>

This my folder structure


My Error:
TemplateSyntaxError at /category/2/
‘news_tags’ is not a registered tag library


2. Describe what you’ve tried

I looked at this question. But there was an error in an unclosed quote

I looked at this question. I write in settings.py TEMPLATES.options.context_processors 'mainsite.news.template_tags.news_tags',. But error No module named 'mainsite.news'

>Solution :

The folder name must be templatetags.

Make sure you are not missing any of the following steps:

  1. Create a folder called templatetags at the same level as models.py
    and views.py in your application folder

  2. Your application must be in the INSTALLED_APPS in settings.py

  3. The templatetags folder must have __init__.py

  4. Restart the django server

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading