Ejemplo n.º 1
0
def show(request, id, slug='a'):
    recipe = get_object_or_404(Recipe, id=id)
    categories = Category.objects.all()
    ingredients = recipe.ingredients.splitlines()
    comments = Comment.objects.filter(recipe=recipe).order_by('-date')

    if request.method == 'POST':
        form = AddCommentForm(request.POST)

        if form.is_valid():
            comment = Comment()
            comment.text = form.cleaned_data['text']
            comment.author = request.user
            comment.recipe = recipe
            comment.date = datetime.now()
            comment.save()
    else:
        form = AddCommentForm()

    return render(request, 'recipe/show.html', {'form': form,
                                                'recipe': recipe,
                                                'categories': categories,
                                                'currentCat': recipe.category,
                                                'ingredients': ingredients,
                                                'comments': comments
                                                })