Sunday, February 28, 2016

Re: Absolute beginner question -- recipes

Thank you Rahul,

I actually tried that an this is possible for the relationship between a recipe and its steps. But when it comes to the ingredients for a single step, I cannot do this any more since the ingredients depend on steps again. After reading your suggestion I thought about it once more and I came up with the following solution:

def detail(request, pk):
    recipe
= Recipe.objects.get(id=pk)
    steps
= Step.objects.filter(recipe_id=pk)

    ingredients
= dict()
   
for step in steps:
        ingredients
[step.id] = StepIngredients.objects.filter(step_id=step.id)

   
template = loader.get_template('recipe/rezept_detail.html')
    context
= {
       
'recipe': recipe,
       
'steps': steps,
       
'ingredients': ingredients
   
}
   
return HttpResponse(template.render(context, request))

However, when it comes to the view, it does not work any more:
<h1>{{ recipe.name }}</h1>

<table>
{% for step in steps %}

 
<tr>
   
<td>
    {% for ingredient in ingredients.step.id %}
      {{ ingredient.amount }}
    {% endfor %}
   
</td>

   
<td>{{ step.description }}</td>
 
</tr>

{% endfor %}
</table>

According to what I found on google, the django template language cannot deal with accessing dictionary entries using variables as keys. What is the django-way of solving this problem? Any ideas?

Thank you!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c0974be2-ad5b-44bd-b08b-2fa0a18eee5a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment