def recipe_edit(request, recipe_id): recipe = get_object_or_404(Recipe, id=recipe_id) if request.user != recipe.author: return redirect('index') if request.method == "POST": form = RecipeForm(request.POST or None, files=request.FILES or None, instance=recipe) ingredients = get_ingredients(request) if form.is_valid(): RecipeIngredient.objects.filter(recipe=recipe).delete() recipe = form.save(commit=False) recipe.author = request.user recipe.save() for item in ingredients: RecipeIngredient.objects.create( units=ingredients[item], ingredient=Ingredients.objects.get(name=f'{item}'), recipe=recipe) form.save_m2m() return redirect('index') form = RecipeForm(request.POST or None, files=request.FILES or None, instance=recipe) return render(request, 'recipe_edit.html', {'form': form, 'recipe': recipe, })
def recipe_edit(request, recipe_id): """ Редактирование рецепта """ recipe = get_object_or_404(Recipe, id=recipe_id) if request.user != recipe.author: return redirect('recipe', recipe_id=recipe.id) recipe_form = RecipeForm(request.POST or None, files=request.FILES or None, instance=recipe) ingredients = get_ingredients_from_js(request) if recipe_form.is_valid(): RecipeIngredient.objects.filter(recipe=recipe).delete() recipe = recipe_form.save(commit=False) recipe.author = request.user recipe.save() recipe.quantities.all().delete() for title, quantity in ingredients.items(): ingredient = get_object_or_404(Ingredient, title=title) recipe_ingredient = RecipeIngredient(quantity=quantity, ingredient=ingredient, recipe=recipe) recipe_ingredient.save() recipe_form.save_m2m() return redirect('index') return render(request, 'recipe_change_form.html', { 'form': recipe_form, 'recipe': recipe, })
def recipe_new(request): """ Создание рецепта """ author = get_object_or_404(User, username=request.user) recipe_form = RecipeForm(request.POST or None, files=request.FILES or None) ingredients = get_ingredients_from_js(request) if recipe_form.is_valid(): recipe = recipe_form.save(commit=False) recipe.author = author recipe.save() for title, quantity in ingredients.items(): ingredient = get_object_or_404(Ingredient, title=title) recipe_ingredient = RecipeIngredient(quantity=quantity, ingredient=ingredient, recipe=recipe) recipe_ingredient.save() recipe_form.save_m2m() return redirect('index') return render(request, 'recipe_form.html', { 'form': recipe_form, })
def new_recipe(request): user = User.objects.get(username=request.user) if request.method == 'POST': form = RecipeForm(request.POST or None, files=request.FILES or None) ingredients = get_ingredients(request) if not ingredients: form.add_error(None, 'Добавьте ингредиенты') elif form.is_valid(): recipe = form.save(commit=False) recipe.author = user recipe.save() for ing_name, amount in ingredients.items(): ingredient = get_object_or_404(Ingredients, title=ing_name) recipe_ing = RecipeIngredient( recipe=recipe, ingredient=ingredient, amount=amount ) recipe_ing.save() form.save_m2m() return redirect('index') else: form = RecipeForm() return render(request, 'new_recipe.html', {'form': form})