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:
Create a folder called
templatetags
at the same level asmodels.py
andviews.py
in your application folderYour application must be in the
INSTALLED_APPS
insettings.py
The
templatetags
folder must have__init__.py
Restart the django server