Sunday, February 28, 2016

Re: Absolute beginner question -- recipes


You could do something like this:

Instead of 

recipe['steps'] = Steps.objects.filter(recipe_id=pk)

Do this:

steps = Steps.objects.filter(recipe_id=pk)
context
= {
       
'recipe': recipe,

   
}





On Monday, February 29, 2016 at 1:00:25 AM UTC+5:30, Simon Gunacker wrote:
Dear Django Community,

as a first django project I tried to write a site to manage cooking recipes. My model is quite simple:

* 1 recipe has n steps
* 1 step has a description and n ingredients, each of a certain amount defined by a certain unit

all in all, I have 5 tables (Recipe, Step, Ingredient, Unit, StepIngredient).

After reading through some documentation and trying around a little bit, I got my model and my admin site (I had to install django-nested-inline for the admin site to fit my expectations).

Now I am struggeling with my views. There is a simple index view wich basically lists all recipes:

class IndexView(generic.ListView):
    context_object_name
= 'recipes_list'

   
def get_queryset(self):
       
return Recipe.objects.all()

The hard part is setting up the details view. It is supposed to show the recipes name as a title and then list all the required ingredients and all the required steps. It tried something like:

def detail(request, pk):
    recipe
= Recipe.objects.get(id=pk)
    recipe
['steps'] = Steps.objects.filter(recipe_id=pk)
   
template = loader.get_template('recipe/recipe_detail.html')
    context
= {
       
'recipe': recipe,
   
}
   
return HttpResponse(template.render(context, request))

but it seems I am not allowed to modify the recipes object. Any ideas how to pass all the data which belongs to recipe to the template?

Thank you,

Simon

--
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/90fe91f6-f752-45aa-9ea9-04d16da71178%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment