请我有一个问题,我想从我的表单中检查两个日期之间的差异,然后如果日期大于其他属性,则允许使用我的类 based_View 创建对象,如果不将其渲染到其他页面而不插入任何内容在数据库中。这是我的
# view
class AddVacation(LoginRequiredMixin, CreateView):
form_class = VacationCreateForm
template_name = 'vacation.html'
login_url = 'login'
def form_valid(self, form):
instance = form.save(commit=False)
instance.employee = self.request.user
return super(AddVacation, self).form_valid(form)
# form:
class VacationCreateForm(forms.ModelForm):
class Meta:
model = VacationModel
fields = [
'type',
'startDate',
'enddate',
]
class VacationCreateForm(forms.ModelForm):
class Meta:
model = VacationModel
fields = [
'type',
'startDate',
'enddate',
]
def clean(self):
start_date = self.cleaned_data['start_date']
end_date = self.cleaned_data['end_date']
from dateutils import parse
start_date = parse(start_date)
end_date = parse(end_date)
# make your calculations
raise forms.ValidationError("message.")
class AddVacation(LoginRequiredMixin, CreateView):
form_class = VacationCreateForm
template_name = 'vavcation.html'
login_url = 'login'
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance with the passed
POST variables and then checked for validity.
"""
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
redirect(to_ur_another_view)
我不是在解决您的具体问题,我只是提供想法或您可以做些什么来跳过您的问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句