示例#1
0
def flavour_add(request):
    if request.method == "POST":
        form = FlavourForm(request.POST)
        if form.is_valid():
            form.save()

            messages.success(request, "Flavour added.")

            return HttpResponseRedirect(reverse("icecream.views.flavours"))

        messages.error(request, "The data is not valid, so the new flavour was not added.")
    else:
        form = FlavourForm()

    return render_to_response("icecream/flavour-add.html", {"form": form}, context_instance=RequestContext(request))
示例#2
0
def flavour_edit(request, id):
    flavour = get_object_or_404(Flavour, pk=id)
    if request.method == "POST":
        form = FlavourForm(request.POST, instance=flavour)
        if form.is_valid():
            form.save()

            messages.success(request, "Flavour changed.")

            return HttpResponseRedirect(reverse("icecream.views.flavours"))

        messages.error(request, "The data is not valid, so the flavour was not updated.")
    else:
        form = FlavourForm(instance=flavour)  # bound form, loaded with data from the db

    return render_to_response(
        "icecream/flavour-edit.html", {"flavour": flavour, "form": form}, context_instance=RequestContext(request)
    )