我想在员工申请的数据库中保存一个假期,但是当我提交时,我遇到了这个错误---- LeaveApplyForm'对象没有属性'save'---
**models.py**
class All_Leaves(models.Model):
leave_types = models.ForeignKey(Leave_Types, on_delete=models.CASCADE, related_name="all_leave")
user = models.ForeignKey(MyUser, on_delete=models.CASCADE, related_name="all_leave")
reason = models.CharField(('Reason'), max_length=120)
start_date = models.DateTimeField(('Start Date'),auto_now = True)
end_date = models.DateTimeField(('End Date'),auto_now = True)
class Meta:
verbose_name = ("All Leave")
verbose_name_plural = ("All Leaves")
**View.py**
def apply_leave(request):
print("user",request.user)
print("data",request.POST)
leave_type_data = Leave_Types.objects.all()
if request.method =="POST":
form = LeaveApplyForm(request.POST, None)
print("form status",form.is_valid())
if form.is_valid():
form.save(leave_types=request.POST['leave_types'], user=request.user)
print("form in")
return render(request, 'dashboard.html',{"var_leave":"false","leave_type_data":leave_type_data})
else:
print("form errors",form.errors)
return render(request, 'dashboard.html',{"var_leave":"false","leave_type_data":leave_type_data,"form":form})
forms.py 类LeaveApplyForm(forms.Form): start_date = forms.DateTimeField(必填=真,error_messages = {'必填':'请选择开始日期。'}) end_date = Forms.DateTimeField(required = True,error_messages = {'required':'请选择结束日期。'}) 原因= form.CharField(required = True,error_messages = {'required':'请输入您的原因。'})
class Meta:
model = All_Leaves
fields = ('start_date','end_date','reason')
答案 0 :(得分:0)
您是从forms.Form
继承而来的,而不是forms.ModelForm
。