How can I submit django form after HTML Form confirmation message.
Model Code:
class Deposit(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
acct = models.CharField(max_length=6, null=True)
staff = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
deposit_amount = models.PositiveIntegerField(null=True)
date = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
return reverse('create_account', args=[self.id])
def __str__(self):
return f'{self.customer} Deposited {self.deposit_amount} by {self.staff.username}
class CustomerDepositForm(forms.ModelForm):
class Meta:
model = Deposit
fields = ['deposit_amount']
I am working on a Saving App where I want user to add Customer Deposits after displaying a confirmation form with message. I have tried all I could but I realised that the Deposit function is working fine and displaying the confirmation message but on click confirm on the confirmation form it is not able to submit the deposit form, rather the system redirects me back to deposit form again; displaying an error on the input form that amount Field Can Not be Empty.
Model Code:
class Deposit(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
acct = models.CharField(max_length=6, null=True)
staff = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
deposit_amount = models.PositiveIntegerField(null=True)
date = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
return reverse('create_account', args=[self.id])
def __str__(self):
return f'{self.customer} Deposited {self.deposit_amount} by {self.staff.username}'
Form Code:
class CustomerDepositForm(forms.ModelForm):
class Meta:
model = Deposit
fields = ['deposit_amount']
Views Code:
def customer_deposit(request, id):
context = {}
form = CustomerDepositForm(request.POST or None)
#Set Page Title
page_title = "Customer Deposit"
#Get Current Date
current_date = datetime.now().date()
#Get Current Month Name from Calendar
current_month_name = calendar.month_name[date.today().month]
try:
#Check the Customer ID in DB
customer = Customer.objects.get(id=id)
#Customer Account
acct = customer.account_number
except Customer.DoesNotExist:
messages.error(request, 'Customer Does Not Exist')
return redirect('customer_create')
else:
#Get the Customer total deposit
deposit = Deposit.objects.filter(customer_id = id).aggregate(total=Sum('deposit_amount')
)['total'] or Decimal()
if request.method == 'POST':
#Deposit Form
form = CustomerDepositForm(request.POST or None)
if form.is_valid():
amount = form.cleaned_data['deposit_amount']
context.update( {
'deposit':deposit,
'page_title':page_title,
'customer':customer,
'current_date':current_date,
'current_month_name':current_month_name,
'form':form,
'amount':amount,
'acct':acct,
})
return render(request, 'dashboard/deposit_approval_form.html', context)
else:
form = CustomerDepositForm()
context = {
'deposit':deposit,
'page_title':page_title,
'customer':customer,
'current_date':current_date,
'current_month_name':current_month_name,
'form':form,
'acct':acct,
}
return render(request, 'dashboard/deposit.html', context)
def approve_deposit(request, id):
user = request.user
form = CustomerDepositForm(request.POST or None)
amount = form.cleaned_data['deposit_amount'].value()
try:
#Check the Customer ID in DB
customer = Customer.objects.get(id=id)
#Customer Account
acct = customer.account_number
except Customer.DoesNotExist:
messages.error(request, 'Customer Does Not Exist')
return redirect('customer_create')
else:
if request.method == 'POST':
#Create Customer Deposit
credit_acct = Deposit.objects.create(customer=customer, acct=acct, staff=user, deposit_amount=amount)
credit_acct.save()
messages.success(request, f'N{amount} Credited for Account {acct} Successfully.')
return redirect('deposit-slip')
else:
form = CustomerDepositForm()
return render(request, 'dashboard/deposit_approval_form.html')
<form method="POST">
{% csrf_token %}
<a class="btn btn-secondary" href="{% url 'create-deposit' customer.id %}">Cancel</a>
<input class="btn btn-danger" type="submit" value="Confirm">
</form>
Confirmation form is displayed but the form is not submitted after submission and redirected back to input form that it’s empty