I’ve been looking at this for over an hour and can’t put my finger on the problem, so I thought it was time to have someone else take a look.
I have a model form that I am presenting through a template. The form seems to be setup properly, and the field that I need to enter/update renders correctly.
Stepping through the debugger up to the initial render, all looks good, and the template renders as expected.
However, when I click the [Submit] button nothing happens. I put a stop in the debugger and it never gets there. It’s as if for some reason, the submit button is being ignored.
Here is the model form setup.
class PayrollSingleCheckForm(ModelForm):
class Meta:
model = PayrollPaychecks
fields = ['payroll_employee', 'check_amount', 'payment_type', 'deposit_complete', 'check_printed',
'check_number', 'payment_voided']
widgets = {
'check_number': forms.TextInput(attrs={'style': 'width: 200px;'}),
}
def __init__(self, *args, check_number=None, **kwargs):
super().__init__()
self.fields['payroll_employee'].widget.attrs['hidden'] = True
self.fields['check_amount'].widget.attrs['hidden'] = True
self.fields['payment_type'].widget.attrs['hidden'] = True
self.fields['deposit_complete'].widget.attrs['hidden'] = True
self.fields['check_printed'].widget.attrs['hidden'] = True
self.fields['payment_voided'].widget.attrs['hidden'] = True
if check_number:
self.fields['check_number'].initial = check_number
Here is the view that renders the form, and that should accept the posted form:
def paycheck_single_check(request, pk):
paycheck_entry = PayrollPaychecks.objects.get(pk=pk)
payroll = paycheck_entry.payroll_employee.payroll
employee = paycheck_entry.payroll_employee.employee
check_setup = employee.department.check_setup
if request.method == 'POST':
form = PayrollSingleCheckForm(request.POST, instance=paycheck_entry)
if form.is_valid():
form.save()
# Create a file-like buffer to receive PDF data.
buffer = io.BytesIO()
# Create the PDF object, using the buffer as its "file."
pdf_check = canvas.Canvas(buffer, pagesize=LETTER, initialFontSize=12)
pdf_check.setFont('Courier', 12)
# Create the check
paycheck_create(paycheck_entry.id, pdf_check)
return redirect('payroll_register', pk=payroll.id)
last_check_number = check_setup.last_check_number
if not last_check_number:
last_check_number = 999
elif last_check_number == 0:
last_check_number = 999
last_check_number = last_check_number + 1
check_position = 'Top'
if check_setup.check_position == 'M':
check_position = 'Middle'
elif check_setup.check_position == 'B':
check_position = 'Bottom'
form = PayrollSingleCheckForm(instance=paycheck_entry, check_number=last_check_number)
context = {
'employee_name': f'{employee.last_name}, {employee.first_name}',
'department': employee.department.name,
'check_amount': paycheck_entry.check_amount,
'check_type': check_setup.check_description,
'check_position': check_position,
'payroll_id': payroll.id,
'pk': pk,
'form': form,
}
return render(request, 'PayrollChecks\paycheck_single_check.html', context)
Here is the template:
{% extends 'main.html' %}
{% block title %}
Single Check - {{ employee_name }}
{% endblock title %}
{% block content %}
{% load static %}
<div class="container rounded bg-white mt-5 mb-5">
<div> </div>
<div class="h5 text-white bg-secondary rounded-corners px-3 pt-1 pb-1">
{{ employee_name }} - Create Paycheck
</div>
<div class="row py-3">
<div class="col-md-12 border-right">
<a href="{% url 'payroll_register' payroll_id %}">
<button class="btn btn-primary button-wide" type="button">To Payroll Register</button></a>
</div>
</div>
<form class="form" method="POST">
{% csrf_token %}
<div class="row">
<div class="col-md-4 border-right">
<div class="row">
<div class="col-md-4"> </div>
</div>
<div class="row">
<div class="col-md-4"> </div>
</div>
<div class="row mt-3">
<div class="col-md-8">
<div
class="d-flex flex-column align-items-center text-center"
>
<img
class="rounded"
width="150px"
height="100px"
alt="Payroll Employees"
src='{% static "images/handtruck_box_move.png" %}'
/>
</div>
</div>
</div>
</div>
<div class="col-md-8 border-right ">
<div class="row">
<div class="col-md4">
<label for="Employee" class="form-label text-primary">Department</label>
<div class="form__field px-3">
{{ department }}
</div>
</div>
</div>
<div class="row">
<div class="col-md4">
<label for="EarningsType" class="form-label text-primary">Check Amount</label>
<div class="form__field px-3">
${{ check_amount|floatformat:"2g" }}
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<label for="Hours" class="form-label text-primary">Check Type</label>
<div class="form__field px-3">
{{ check_type }}
</div>
</div>
<div class="col-md-4">
<label for="Rate" class="form-label text-primary">Check Postion</label>
<div class="form__field px-3">
{{ check_position }}
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<label for="Memo" class="form-label text-primary">Check Number</label>
<div class="form__field px-3">
{{ form.check_number }}
</div>
</div>
</div>
{# Hidden fields - required to save the set check number #}
{{ form.payroll_employee }}
{{ form.check_amount }}
{{ form.payment_type }}
{{ form.deposit_complete }}
{{ form.check_printed }}
{{ form.payment_voided }}
<div class="row mt-3 mb-3">
<div class="col-md-12">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 border-right"></div>
<div class="col-md-8 border-right">
<button type="submit" class="btn btn-primary">Create Check</button>
</div>
</div>
<div class="row mt-5"></div>
</form>
</div>
{% endblock content %}
I’m open to any suggestions on why this form will not submit. I have dozens of other forms setup very similar with no issues, so I must be overlooking something here, but I can’t put my finger on it.
Thanks in advance for any help you can provide. No doubt, this is something very simple that I am just missing!