title: Dynamically generate template (context) objects in django view
body: <p>I'm currently doing something like this in my views.py:</p>
  
  <pre><code>def home(request):
      ingredients = Ingredience.objects.all()
      drinks = Ingredience.objects.filter(category_id=1)
      fruits =  Ingredience.objects.filter(category_id=2)
      etc.
      meats = Ingredience.objects.filter(category_id=25)
  
      return render_to_response('home.html', {'ingredients': ingredients, 'drinks': drinks, 'fruits': fruits, 'meats': meats,}, context_instance=RequestContext(request))
  </code></pre>
  
  <p>And this is my models.py:</p>
  
  <pre><code>from django.db import models
  
  class IngredienceCategory(models.Model):
      name = models.CharField(max_length=30)
  
      def __unicode__(self):
          return self.name
  
  
  class Ingredience(models.Model):
      name = models.CharField(max_length=30)
      category = models.ForeignKey(IngredienceCategory, null=True, blank=True)
  
      def __unicode__(self):
          return self.name
  
  
  class Food(models.Model):
      name = models.CharField(max_length=30)
      ingredients = models.ManyToManyField(Ingredience)
  
      def __unicode__(self):
          return self.name
  </code></pre>
  
  <p>Is there some trick in django to generate the template objects (i.e. drinks, fruits, meats) dynamically? I don't even know where to start but I thought doing something like this: loop through all ingredients objects and return dictionary which would hold items grouped by category (as in the first example).</p>
  
  <p>There are 2 problems I'm trying to solve with this:</p>
  
  <ol>
  <li>The code for each category object is almost the same so there is a
  lot of redundancy. </li>
  <li>I have to update views.py every time I add new
  category to the database.</li>
  </ol>
  
  <p><strong>EDIT:</strong> In my template I need to be able to access each object (i.e. drinks, fruits, meats etc.) by its own variable (e.g. {{ drinks }}.</p>