ATM machine demo with Python and Django

Here is my latest views function:

def index(request):
   # Starting balance variable initialization:
   balance = 0
   context = {'balance': balance}
   data = Account.objects.all() 
   myaccount = Account.objects.get(id=1)
   print(request.POST)
   if request.method == 'POST':
       form = Transactions(request.POST)
       if form.is_valid():
           print(form)
           amount = form.cleaned_data['amount']
           if request.POST['transaction'] == 'Deposit':
               balance = balance + amount
               context.update({'balance': balance,})
           if request.POST['transaction'] == 'Withdraw':
               balance = balance - amount
               context.update({'balance': balance,})
           myaccount.balance = balance
           myaccount.save()
           return render(request, 'telagents/home.html', {'form': form, 'data':data, 'context': context,})
 
   else:
       form = AmountForm()
 
   return render(request, 'telagents/home.html', {'form': form, 'data':data, })

The crucial changes above is the instantiation of the Account class object during initialization with the .get query set to the only id row entry in the db. I then refer to this object towards the end of the algorithm where I apply the balance input collected from the POST request to the balance attribute and then save it to myaccount.

Any further commentary to clarify that understanding from other forum members might help.

So I accomplished what I intended to in my post above. By that I mean, when a web visitor lands on the web page now and they enter a dollar amount and click “Deposit”, the balance field in the ledger updates to reflect the amount entered.

The problem to solve next is that my views.py function algorithm doesn’t track a running tally with subsequent deposits. When a web visitor enters a second deposit amount, it just overwrites the former balance. The next step for me to take is to fix this.

I had help from a friend to come up with my revised views.py but I refined it further based on a free mini crash course which teaches how to first use a GUI sqlite browser utility and then proceeds to cover querying db.sqlite3 from Python REPL integrated with a basic Django test project. The course refers to Django docs:

1 Like