what i want is to make the DateField and CharField required so that the form cant be submitted with the fields empty, but even after setting required = True in both it doesn't take effect and end up throwing an error on saving the form, i dnont know where am getting it wrong
here is the form class:
class RepairOrderForm(forms.ModelForm):
brand = forms.CharField(
required=True,
widget=forms.TextInput(
attrs={
"placeholder": " e.g DELL",
}
)
)
model = forms.CharField(
required=True,
widget=forms.TextInput(
attrs={
"placeholder": "Device model",
}
)
)
serial_no_or_IMEI = forms.CharField(
required=True,
label="Serial No/IMEI",
widget=forms.TextInput(
attrs={
"placeholder": "Device serial No/EMEI",
}
)
)
fault_details = forms.CharField(
required=True,
widget=forms.Textarea(
attrs={
"placeholder": " A detailed description of the fault",
}
)
)
pick_up_date = forms.DateField(
required=True,
# initial = datetime.date.today,
widget=forms.DateTimeInput(
attrs={
# "class": "datepicker",
}
)
)
coupon_code = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={
"placeholder": "Got a coupon/voucher use it here and get a discount on your repair order",
}
)
)
survey = forms.CharField(
required=False,
widget=forms.Textarea(
attrs={
"placeholder": "How did you hear about us",
}
)
)
terms_and_condition = forms.BooleanField(
required=True,
label=' I have read and agree to the'
)
class Meta:
model = RepairOrderModel
fields = [
'device_type',
'brand',
'model',
'serial_no_or_IMEI',
'fault_details',
'pick_up_date',
'coupon_code',
'survey',
'terms_and_condition'
]
and here is the model:
class RepairOrderModel(models.Model):
device_type_choices = [
('LAPTOP', 'LAPTOP'),
('PHONE', 'PHONE'),
('DESKTOP', 'DESKTOP'),
('WRISTWATCH', 'WRISTWATCH'),
('DRONE', 'DRONE'),
('LED/LCD TV', 'LED/LCD TV'),
('OTHERS', 'OTHERS'),
]
order_tracking_choices = [
('PENDING', 'PENDING'),
('PICKED UP', 'PICKED UP'),
('REPAIR IN PROGRESS', 'REPAIR IN PROGRESS'),
('REPAIR DONE', 'REPAIR DONE'),
('READY FOR DELIVERY', 'READY FOR DELIVERY'),
('DELIVERED','DELIVERED')
]
owner = models.ForeignKey(User, on_delete=models.CASCADE)
order_id = models.CharField(max_length=120, default="")
device_type = models.CharField(max_length=20, choices = device_type_choices, default = 'LAPTOP')
brand = models.CharField(max_length=120, default="")
model = models.CharField(max_length=120, default="")
serial_no_or_IMEI = models.CharField(max_length=120, default="")
fault_details = models.TextField()
pick_up_date = models.DateField()
order_tracking = models.CharField(max_length=50, choices = order_tracking_choices, default = 'PENDING')
coupon_code = models.CharField(max_length=20, default="")
survey = models.TextField()
terms_and_condition = models.BooleanField(default = False)
order_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return f'{self.owner.username} Repair Order'
and here is the view:
@login_required
def RepairOrderView(request):
page_tittle = "Repair Order"
if request.method == 'POST':
p_u_form = ProfileUpdateForm(
request.POST, instance=request.user.profile)
repair_order_form = RepairOrderForm(request.POST)
repair_order_form.instance.owner = request.user
if repair_order_form and p_u_form.is_valid():
p_u_form.save()
repair_order_form.save()
username = request.user
messages.success(
request,
f'{username}, Thanks for placing a repair order . Our customer care will be contacting you soon, to confirm and process your request'
)
odr_id = RepairOrderModel.objects.all().filter(
owner=request.user).order_by('-order_date')[0].order_id
content = f"Thanks for Placing a repair order. Your Order ID is {odr_id} . Our customer care will be contacting you soon, to confirm and process your request"
html_msg = render_to_string('repairsquad_home_app/email_templates/email.html', context={
'username': request.user.username,
'content': content,
})
send_mail(
"REPAIR SQUAD NOTIFICATION",
f"Hi {request.user.username} Thanks for Placing a repair order. Your Order ID is {odr_id} . Our customer care will be contacting you soon, to confirm and process your request ",
settings.EMAIL_HOST_USER,
[request.user.email],
fail_silently=True,
html_message=html_msg,
)
admin_content = f"THE USERNAME ABOVE PLACE A REPAIR ORDER WITH ORDER ID {odr_id} . PLEASE CHECK IT UP AND PROCESS"
admin_html_msg = render_to_string('repairsquad_home_app/email_templates/email.html', context={
'username': request.user.username,
'content': admin_content,
})
send_mail(
"REPAIR SQUAD NOTIFICATION",
f"THE USERNAME ABOVE PLACE A REPAIR ORDER WITH ORDER ID {odr_id} . PLEASE CHECK IT UP AND PROCESS",
settings.EMAIL_HOST_USER,
[settings.EMAIL_HOST_USER],
fail_silently=True,
html_message=admin_html_msg,
)
return redirect('profile')
else:
messages.error(
request,
f'{username}, Please fill the pickup date'
)
return redirect('profile')
else:
repair_order_form = RepairOrderForm()
p_u_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'page_tittle': page_tittle,
'repair_order_form': repair_order_form,
'p_u_form': p_u_form
}
return render(request, 'repairsquad_home_app/repair_order_form.html', context)
You need to call .is_valid()
on the repair_order_form
as well. By writing if repair_order_form and p_u_form.is_valid():
you check the truthiness of the RepairOrderForm
. Since the RepairOrderForm
does not override __bool__
, it means that it will always be True
.
You thus write the view as:
if request.method == 'POST':
p_u_form = ProfileUpdateForm(
request.POST, instance=request.user.profile)
repair_order_form = RepairOrderForm(request.POST)
repair_order_form.instance.owner = request.user
if repair_order_form.is_valid() and p_u_form.is_valid():
# …