示例#1
0
def drink_detail(request, drink_id):
    drink = get_object_or_404(models.Drink, id=drink_id)
    context = RequestContext(request, {'drink': drink})

    can_delete = (request.user == drink.user) or request.user.is_staff

    if can_delete:
        picture_form = forms.DeletePictureForm(initial={'picture': drink.picture})
    else:
        picture_form = None

    if request.method == 'POST':
        if can_delete:
            picture_form = forms.DeletePictureForm(request.POST)
            if picture_form.is_valid():
                drink.picture.erase_and_delete()
                picture_form = None
                messages.success(request, 'Erased image.')
            else:
                messages.error(request, 'request not valid: ' + str(picture_form.errors))
        else:
            messages.error(request, 'No permission to delete picture.')
        return redirect('kb-drink', drink_id=str(drink_id))

    context['picture_form'] = picture_form
    return render_to_response('kegweb/drink_detail.html', context_instance=context)
示例#2
0
def drink_detail(request, drink_id):
    drink = get_object_or_404(models.Drink, id=drink_id)
    context = {
        "drink": drink,
    }

    can_delete = (request.user == drink.user) or request.user.is_staff

    if can_delete:
        picture_form = forms.DeletePictureForm(initial={"picture": drink.picture})
    else:
        picture_form = None

    if request.method == "POST":
        if can_delete:
            picture_form = forms.DeletePictureForm(request.POST)
            if picture_form.is_valid():
                drink.picture.erase_and_delete()
                picture_form = None
                messages.success(request, "Erased image.")
            else:
                messages.error(request, "request not valid: " + str(picture_form.errors))
        else:
            messages.error(request, "No permission to delete picture.")
        return redirect("kb-drink", drink_id=str(drink_id))

    context["picture_form"] = picture_form
    return render(request, "kegweb/drink_detail.html", context=context)