def edit(request, recipe_id): recipe = get_object_or_404(Recipe, pk=recipe_id, owner=request.user) if request.method == 'POST': recipe_form = RecipeForm(request.POST, instance=recipe) if recipe_form.is_valid(): updated_recipe = recipe_form.save(commit=False) updated_recipe.owner = request.user updated_recipe.manually_edited = True updated_recipe.save() recipe_form.save_m2m() return redirect('/recipes/%s' % recipe_id) else: recipe_form = RecipeForm(instance=recipe) recent_recipes = Recipe.objects.filter(owner=request.user).order_by('-added')[:5] popular_recipes = Recipe.objects.filter(owner=request.user).order_by('-made_count')[:5] return render_to_response( 'edit_recipe.html', { 'recipe': recipe, 'recipe_form': recipe_form, 'recent_recipes': recent_recipes, 'popular_recipes': popular_recipes, }, context_instance = RequestContext(request) )
def index(request): if request.method == 'POST': add_form = RecipeForm(request.POST) if add_form.is_valid(): recipe = add_form.save(commit=False) recipe.owner = request.user recipe.source = "web" recipe.save() add_form.save_m2m() return redirect(reverse('recipemanager.view.all')) else: add_form = RecipeForm() recent_recipes = Recipe.objects.filter(owner=request.user).order_by('-added')[:5] popular_recipes = Recipe.objects.filter(owner=request.user).order_by('-made_count')[:5] return render_to_response( 'recipes.html', { 'add_form': add_form, 'recent_recipes': recent_recipes, 'popular_recipes': popular_recipes, }, context_instance=RequestContext(request) )
def add(request): if request.method == 'POST': form = RecipeForm(request.POST) if form.is_valid(): recipe = form.save(commit = False) recipe.owner = request.user recipe.source = "web" recipe.save() form.save_m2m() else: pass return redirect('/recipes')