Why django generates empty a tags in html templates when nested. Only in ul area and in 1st li child.
test.html:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<nav class="site-nav">
<ul class="nav-ul">
{% for item in nav %}
<li class="nav-li">
<a class="nav-a" href="{% url item.url %}">
{{ item.title }}
{% if item.nested %}
<ul class="expand-ul">
{% for nested_item in item.nested %}
<li class="nested-li">
{% if nested_item.tag %}
<a href="{% url nested_item.url service_tag=nested_item.tag %}" class="nested-a">
{{ nested_item.title }}
</a>
{% else %}
<a href="{{ nested_item.url }}" class="nested-a">
{{ nested_item.title }}
</a>
{% endif %}
</li>
{% endfor %}
</ul>
{% endif %}
</a>
</li>
{% endfor %}
</ul>
</nav>
</body>
/=================================================================/
views.py:
from .test_utils import TestMixin
class TestPage(TestMixin, TemplateView):
template_name = 'test.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
mix_context = self.get_mixin_context()
context.update(mix_context)
print('vws>test>context>>>>:', context)
return context
/=========================================================================/
test_utils.py:
services = [
{
'title': 'repair flat',
'url': 'services',
'tag': 'repair_flat'
},
{
'title': 'repair house',
'url': 'services',
'tag': 'repair_house'
},
{
'title': 'repair prom',
'url': 'services',
'tag': 'repair_prom'
},
]
contacts = [
{
'title': 'info@mysite.com',
'url': 'mailto: info@mysite.com',
},
{
'title': 'google',
'url': 'https://google.com/',
},
]
nav = [
{
'title': 'HOME',
'url': 'home',
},
{
'title': 'ABOUT',
'url': 'about',
},
{
'title': 'SERVICES',
'url': 'home',
'nested': services
},
{
'title': 'CONTACTS',
'url': 'home',
'nested': contacts
}
]
class TestMixin:
def get_mixin_context(self):
context = dict()
context['nav'] = nav
return context
/========================================================================/
when “a” tag not nested it work ok: