Ejemplo n.º 1
0
def edit_meal(request, meal_id):
    context_dict = {}

    user = User.objects.get(username=request.user)

    # Check to see if meal exists.
    meal = get_object_or_404(Meal, pk=meal_id)

    # Check to see if user has access to that meal.
    if user != meal.user:
        raise PermissionDenied

    if request.method == "POST":
        form = MealForm(request.POST, instance=meal)
        if form.is_valid():
            meal = form.save()
            messages.success(request, "Your meal has been updated.")
            return HttpResponseRedirect("/meal_tracker/dashboard/")
    else:
        form = MealForm(instance=meal)

    context_dict["form"] = form
    context_dict["page_title"] = "Edit Meal"
    context_dict["h1_header"] = "Edit Your Meal"
    context_dict["form_action_tag"] = "edit_meal/" + meal_id
    return render(request, "meal_tracker/add_edit_meal.html", context_dict)
Ejemplo n.º 2
0
def add_meal(request):
    context_dict = {}

    user = User.objects.get(username=request.user)

    if request.method == "POST":
        meal = Meal(user=user)
        form = MealForm(request.POST, instance=meal)
        if form.is_valid():
            meal = form.save()
            messages.success(request, "Your meal has been added.")
            return HttpResponseRedirect("/meal_tracker/dashboard/")
        else:
            print form.errors
    else:
        form = MealForm()

    context_dict["form"] = form
    context_dict["page_title"] = "Add Meal"
    context_dict["h1_header"] = "Add a New Meal"
    context_dict["form_action_tag"] = "add_meal"
    return render(request, "meal_tracker/add_edit_meal.html", context_dict)