def expense_edit(request, pk): expense = get_object_or_404(Expense, pk=pk, user=request.user) if expense.is_bill: return HttpResponseRedirect(expense.get_absolute_url()) form = ExpenseForm(instance=expense, user=request.user) if request.method == "POST": form = ExpenseForm(request.POST, instance=expense, user=request.user) if form.is_valid(): expense = form.save(commit=False) expense.user = request.user expense.is_bill = False expense.save() form.save_m2m() return HttpResponseRedirect(expense.get_absolute_url()) return render( request, "expenses/expense_add_edit.html", { "htmltitle": str(expense), "pid": "expense_edit", "expense": expense, "form": form, "mode": "edit", }, )
def expense_add(request): form = ExpenseForm(user=request.user) if request.method == "POST": form = ExpenseForm(request.POST, user=request.user) if form.is_valid(): inst = form.save(commit=False) inst.user = request.user inst.is_bill = False inst.save() form.save_m2m() return HttpResponseRedirect(reverse("expenses:expense_list")) return render( request, "expenses/expense_add_edit.html", {"htmltitle": _("Add an expense"), "pid": "expense_add", "form": form, "mode": "add",}, )