Всем привет!!! Перерыл кучу сайтов но правильного решения проблемки так и не нашел. У меня есть простая модель категорий
class Category(MPTTModel):
name = models.CharField(max_length=50, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
def __str__(self):
return self.name
Дерево категорий должно отображаться на всех страницах сайта. т.е. нужно создать шаблонный тег и впилить его в базовый шаблон. Делаю следующее:
В папке templatetags приложения создаю файл category_tegs.py и в нем
from django import template
from django.shortcuts import render_to_response
from apps.core.models import Category
register = template.Library()
@register.inclusion_tag('category_tree.html', takes_context=True)
def show_category(context):
category = Category.objects.all()
return render_to_response('category_tree.html', {'nodes': category}, context_instance=context)
Далее создаю шаблон category_tree.html
{% load category_tags %}
{% load mptt_tags %}
{% block category %}
<ul>
{% recursetree nodes %}
<li>
{{ node.name }}
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
{% endblock %
}
Ну и в шаблон сам тег {% show_category %}
В результате получаю
Template error:
In template /django_projects/hm/hm/apps/core/templates/category_tree.html, error at line 7
Failed lookup for key [%s] in %r
1 : {% load category_tags %}
2 : {% load mptt_tags %}
3 :
4 : {% block category %}
5 :
6 : <ul>
7 : {% recursetree nodes %}
8 : <li>
9 : {{ node.name }}
10 : {% if not node.is_leaf_node %}
11 : <ul class="children">
12 : {{ children }}
13 : </ul>
14 : {% endif %}
15 : </li>
16 : {% endrecursetree %}
17 : </ul>
Traceback:
File "/django_projects/hm/env/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/django_projects/hm/hm/apps/core/views.py" in main_page
31. return render_to_response('index.html', args, context_instance=RequestContext(request))
File "/django_projects/hm/env/lib/python3.4/site-packages/django/shortcuts/__init__.py" in render_to_response
29. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/loader.py" in render_to_string
169. return t.render(context_instance)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/loader_tags.py" in render
123. return compiled_parent._render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/loader_tags.py" in render
62. result = block.nodelist.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in render
1220. return self.nodelist.render(new_context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/loader_tags.py" in render
53. result = self.nodelist.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/mptt/templatetags/mptt_tags.py" in render
316. queryset = self.queryset_var.resolve(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in resolve
735. value = self._resolve_lookup(context)
File "/django_projects/hm/env/lib/python3.4/site-packages/django/template/base.py" in _resolve_lookup
781. (bit, current)) # missing attribute
Exception Type: VariableDoesNotExist at /
Exception Value: Failed lookup for key [nodes] in "[{'True': True, 'None': None, 'False': False}, <django.http.response.HttpResponse object at 0x7fb215fbd278>, {'block': <Block Node: category. Contents: [<Text Node: '\n\n<ul>\n '>, <mptt.templatetags.mptt_tags.RecurseTreeNode object at 0x7fb215f8eac8>, <Text Node: '\n</ul>\n\n'>]>}]"