def create_recipe(request): if request.method == 'GET': form = RecipeForm() return render(request, 'create.html', {'form': form}) else: form = RecipeForm(request.POST) if form.is_valid(): form.save() return redirect('index') return render(request, 'create.html', {'form': form})
def edit_recipe(request, pk): recipe = Recipe.objects.get(pk=pk) if request.method == 'GET': form = RecipeForm(instance=recipe) context = { 'recipe': recipe, 'form': form, } return render(request, 'edit.html', context) else: form = RecipeForm(request.POST, instance=recipe) if form.is_valid(): form.save() return redirect('index') context = { 'form': form, } return render(request, 'edit.html', context)