3

The problem is the header.html partial always contains categories dictionary that is kept on database. Including this partial with arguments

{% include "_partials/header.html" with categories %}

Every time on rendering partials I need to pass categories dictionary

render("index.html", {"flowers":flowers, "categories":categories}) 
render("details.html", {"flower":flower, "categories":categories})
...

Is there any solution, that header.html partials always contains categories dictionary.

maksadbek
  • 1,508
  • 2
  • 15
  • 28
  • Possible duplicate of [how to setup custom middleware in django](http://stackoverflow.com/questions/18322262/how-to-setup-custom-middleware-in-django) – Sayse Nov 28 '15 at 12:14
  • ^ you can create your own middlewares so that you can include it in your request. There are also [context processors](https://docs.djangoproject.com/en/1.8/ref/templates/api/#subclassing-context-requestcontext) – Sayse Nov 28 '15 at 12:16

2 Answers2

4

Solved it using inclusion tags.

Created custom tag in the templatetags/tags.py file

from django import template
from flowers.models import Category
register = template.Library()
@register.inclusion_tag('_partials/nav.html')
def show_categories():
    categories = Category.objects.all()
    print categories
    return {'categories':categories}

Created template for it in the _partials/nav.html file

<nav>
   <ul>
        {% for category in categories %}
            <li><a href="{% url 'category:detail' category.id' %}">{{ category.name }}</a></li>
        {% endfor %}
   </ul>
</nav>

At the end, used that tag

{% load tags %}
{% show_categories %}
maksadbek
  • 1,508
  • 2
  • 15
  • 28
1

You should use a custom inclusion tag for this.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895