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')
Deposit Approval Template code:
<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>
Someone should help with the best way of achieving this. Thanks
You have to use null=True, blank=True
For field which you are not using in form. And then make migrations again
null = True, blank = False
means that field can be empty in DATABASE but NOT IN FORM and vice versa.
From my Observation, you're not submitting the form in the try block
, you are only submitting the form in the else block
. The form will never submit because the try block
is always true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.