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 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:
-
Create a folder called
templatetagsat the same level asmodels.py
andviews.pyin your application folder -
Your application must be in the
INSTALLED_APPSinsettings.py -
The
templatetagsfolder must have__init__.py -
Restart the django server