def deleteExpense(request, pk): try: Expenses.objects.get(id=pk).delete() form = AddExpensForm() context = {} context["form"] = form return redirect("addexpens") # print("delete") except Exception as e: return redirect("addexpens")
def addExpens(request): form=AddExpensForm(initial={"user":request.user}) context={} context["form"]=form expenses=Expenses.objects.filter(user=request.user) context["expenses"]=expenses if request.method=='POST': form=AddExpensForm(request.POST) if form.is_valid(): form.save() return redirect("addexpenses") return render(request,"Budget/addexpens.html",context)
def editExpenses(request,id): expense=Expenses.objects.get(id=id) form=AddExpensForm(instance=expense) context={} context["form"]=form if request.method=='POST': form=AddExpensForm(instance=expense,data=request.POST) if form.is_valid(): form.save() return redirect("addexpenses") else: context["form"]=form return render(request,"Budget/editexpense.html",context) return render(request,"Budget/editexpense.html",context)
def editExpense(request, pk): id = Expenses.objects.get(id=pk) form = AddExpensForm(instance=id) context = {} context["form"] = form if request.method == "POST": form = AddExpensForm(instance=id, data=request.POST) if form.is_valid(): form.save() return redirect("addexpens") # print("save") else: context["form"] = form # print("not save") return render(request, "Budget/editexpense.html", context)