I am new to Wagtail but have some knowledge in Django. I was trying to follow the official documentation of Wagtail but I got stuck in the snippets section. I am trying to render a snippet NavigationComponent in home/templates/home/home_page.html. I have defined a tag in home\templatetags\navigation_tags.py to render it. However when I am trying to load the tag using {% load %}, it doesn't recognize it.
Here is the code:-
[base.py]
INSTALLED_APPS = [
'home',
'search',
'website',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'modelcluster',
'taggit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
###########################################################################################
[home/models.py]
from django.db import models
from wagtail.core.models import Page
from wagtail.snippets.models import register_snippet
from wagtail.admin.edit_handlers import FieldPanel, PageChooserPanel
class HomePage(Page):
pass
@register_snippet
class NavigationComponent(models.Model):
name = models.CharField(max_length=50, blank=False)
link = models.ForeignKey(
'wagtailcore.Page',
null=True,
blank=False,
on_delete=models.SET_NULL,
related_name='+'
)
url = models.URLField(blank=True, null=True)
panels = [
FieldPanel('name'),
PageChooserPanel('link'),
FieldPanel('url')
]
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'Navigation Item Container'
verbose_name = "Navigation Item Container"
@register_snippet
class NavigationItem(models.Model):
name = models.CharField(max_length=50, blank=False)
link = models.URLField()
class Meta:
verbose_name_plural = 'Navigation Items'
[home/templatetags/navigation_tags.py]
from django import template
from ..models import NavigationComponent
register = template.Library()
@register.inclusion_tag('../templates/tags/navigation_component.html', takes_context=True)
def navigation_components(context):
return {
'components': NavigationComponent.objects.all(),
'request': context['request']
}
##########################################################################################
[home/templates/home/home_page.html]
{% extends "base.html" %}
{% load static navigation_tags %} # Unresolved library 'navigation_tags'
{% block body_class %}template-homepage{% endblock %}
{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/welcome_page.css' %}">
{% endblock extra_css %}
{% block navigation %}
{% navigation_components %} # template error :Invalid block tag
{% endblock %}
{% block content %}
{% endblock content %}
templatetags is a python package. I have looked through both Django and Wagtail documentation yet I can't figure out what I am doing wrong.