title: Django View and helper
body: <p>I have a django project (fairly simple) which uses the MPTT package, through it I create categories for specific models.
  I am creating a menu system which is based on the MPTT, but I find myself repeating my code in my views, to be more specific:</p>
  
  <pre><code>def index(request):
  menu = MyCategory.objects.all()
  
  def viewitem(request, item_slug):
  menu = MyCategory.objects.all()
  </code></pre>
  
  <p>Should this be refactored to a template tag? should I make a helper function? Or create a middleware context which will pass the object to the template? Which would be the best implementation according to django logic? from the example its fairly simple that I return only a queryset to be used by the mptt existing tags:</p>
  
  <pre><code>{% if menu.exists %}
  &lt;nav id="topnavigation"&gt;
  &lt;ul&gt;
      {% spaceless %}
      {% recursetree menu %}
      &lt;li&gt;
          &lt;a href="{% url 'view-category' node.slug %}"&gt;{{ node.title }}&lt;/a&gt;
          {% if not node.is_leaf_node %}
          &lt;ul class="submenu"&gt;
              {{ children }}
          &lt;/ul&gt;
          {% endif %}
      &lt;/li&gt;
      {% endrecursetree %}
      {% endspaceless %}
  &lt;/ul&gt;
  &lt;/nav&gt;
  {% endif %}
  </code></pre>