Pregunta

In base.html:

<div id="menu_shop">
            <input name="search" placeholder="search">
            <p>Category:</p>
             {% load mptt_tags %}
<ul class="root">
    {% recursetree nodes %}
        <li>
            {{ node.name }}
            {% if not node.is_leaf_node %}
                <ul class="children">
                    {{ children }}
                </ul>
            {% endif %}
        </li>
    {% endrecursetree %}
</ul>
</div>

in views:

def show_category_tree(request):
    return render_to_response("base.html",
                              {'nodes': Category.tree.all()},
                              context_instance=RequestContext(request))

urls.py:

url(r'^category/', 'item.views.show_category_tree'),
url(r'^category/(?P<slug>[\w\-_]+)/$', 'item.views.by_category'),

How to display this in "by_category.html"

If I try(for example):

{% extends "base.html" %}


{% block content %}
{% for e in entries %}
<p><b>{{ e.name}}</b></p>

<p>{{ e.desc}}</p>
{% endfor %}

{% endblock %}

I have this error:

http://dpaste.com/810809/

{% extends "base.html" %} does not work. If I remove it, everything works.

¿Fue útil?

Solución

You are seeing this error because your template context for the by_category does not include nodes.

The extends tag is related to the template, not the view. It makes your by_category.html template extend the base.html template, but it does not include the template context from any other view.

The easiest fix would be to add nodes to your template context in the by_category view.

def by_category(request, slug):
    entries = Entry.objects.filter(...)
    return render_to_response("base.html",
        {'entries': entries,
         'nodes': Category.tree.all()},
        context_instance=RequestContext(request))

This would be repetitive if you want to display the nodes in lots of other views. If you want to include the nodes in all views, you may want to write a request context processor. If you want to include it in some but not all pages, then try writing a custom template tag.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top