The Payment could not be created because the data didn't validate
I get the following error: The Payment could not be created because the data didn't validate. #view
def home(request):
customer = Customer.objects.all().filter(user=request.user)
payments = Payment.objects.all().filter(user=request.user)
if request.method == "POST":
form = PaymentForm(request.POST)
if form.is_valid:
fs = form.save(commit=False)
fs.user = request.user
fs.save()
messages.add_message(request, messages.INFO, 'ثبت با موفقیت انجام شد')
else:
messages.add_message(request, messages.WARNING, 'خطا در ثبت')
else:
form = PaymentForm()
context = {
'payments': payments,
'customer': customer,
'form': form,
}
return render(request, 'payment/index.html', context)
#model
from django_jalali.db.models import jDateField
class Payment(models.Model):
number = models.IntegerField(verbose_name='شماره', unique=True)
price = models.IntegerField(verbose_name='مبلغ')
date = jDateField()
pardakht = models.ForeignKey(Customer, on_delete=models.CASCADE, verbose_name='دریافت از')
bank = models.IntegerField(choices=bank, verbose_name='بانک')
description = models.TextField(verbose_name='توضیحات', null=True)
status = models.BooleanField(default='0')
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
created_at = models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return f'{self.number}'
#forms
from jalali_date.fields import JalaliDateField, SplitJalaliDateTimeField
from jalali_date.widgets import AdminJalaliDateWidget, AdminSplitJalaliDateTime
from .models import Payment
class PaymentForm(forms.ModelForm):
class Meta:
model = Payment
fields = ['number', 'price', 'date', 'pardakht', 'bank', 'description']
number = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control'}), label='شماره چک')
price = forms.IntegerField(widget=forms.TextInput(attrs={'class': 'form-control'}), label='مبلغ چک')
pardakht = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), label='مشتری')
bank = forms.ChoiceField(widget=forms.Select(attrs={'class': 'form-control'}), choices=bank, label='بانک')
description = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control'}), label='توضیحات')
def __init__(self, *args, **kwargs):
super(PaymentForm, self).__init__(*args, **kwargs)
self.fields['date'] = JalaliDateField(label=('تاریخ چک'), widget=AdminJalaliDateWidget)
index.html
<h4 class="text-center alert alert-info"> ثبت چک جدید </h4>
<form method="post" action="#">
{% csrf_token %}
{# {{ form.as_p }}#}
<div id="div_id_number" class="control-group">
<label for="id_number" class="control-label requiredField">
شماره چک
</label>
<div class="controls">
{{ form.number }}
</div>
</div>
<div id="div_id_price" class="control-group">
<label for="id_price" class="control-label requiredField">
مبلغ چک
</label>
<div class="controls">
{{ form.price }}
</div>
</div>
<div id="div_id_date" class="control-group">
<label for="id_date" class="control-label requiredField">
تاریخ چک
</label>
<div class="controls">
{{ form.date }}
</div>
</div>
<div id="div_id_pardakht" class="control-group">
<label for="id_pardakht" class="control-label requiredField">
مشتری
</label>
<div class="controls">
<select name="pardakht" class="form-control">
<option>-------</option>
{% for item in customer %}
<option value="{{ item.id }}">{{ item.name }}</option>
{% endfor %}
</select>
</div>
</div>
<div id="div_id_bank" class="control-group">
<label for="id_bank" class="control-label requiredField">
بانک
</label>
<div class="controls">
{{ form.bank }}
</div>
</div>
<div id="div_id_description" class="control-group">
<label for="id_description" class="control-label requiredField">
توضیحات
</label>
<div class="controls">
{{ form.description }}
</div>
</div>
<br>
<button type="submit" class="btn btn-success">
<span class="fa fa-plus"></span>
ثبت
</button>
</form>
{{ form.media }}
</div>
Error
ValueError at /check/payment/ The Payment could not be created because the data didn't validate. Request Method: POST Request URL: http://127.0.0.1:8000/check/payment/ Django Version: 4.1.5 Exception Type: ValueError Exception Value: The Payment could not be created because the data didn't validate. Exception Location: E:\python\chourtke.ir\chourtke\env\lib\site-packages\django\forms\models.py, line 539, in save Raised during: payment.views.home Python Executable: E:\python\chourtke.ir\chourtke\env\Scripts\python.exe Python Version: 3.10.4 Python Path: ['E:\python\chourtke.ir\chourtke', 'C:\Users\hamid\AppData\Local\Programs\Python\Python310\python310.zip', 'C:\Users\hamid\AppData\Local\Programs\Python\Python310\DLLs', 'C:\Users\hamid\AppData\Local\Programs\Python\Python310\lib', 'C:\Users\hamid\AppData\Local\Programs\Python\Python310', 'E:\python\chourtke.ir\chourtke\env', 'E:\python\chourtke.ir\chourtke\env\lib\site-packages'] Server time: Sun, 22 Jan 2023 21:50:06 +0000