Home > database >  Django: TemplateSyntaxError at /category/2/
Django: TemplateSyntaxError at /category/2/

Time:04-13

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

{% load news_tags %}
{% get_categories %}

<div >
    {% for item in categories %}
    <a href="{% url 'category' item.pk %}" >{{ 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'

CodePudding user response:

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

  • Related