我有一个包含两个字段的Django表单:
日期字段 - 使用jQuery日历窗口小部件,我可以选择一个 日期
时间字段 - 下拉菜单,时间为上午9:00至下午5:30 间隔30分钟(上午9:00,上午9:30等)
现在,根据用户在日期字段中选择的日期,我想限制时间。例如,如果用户选择明天,即今天下午2:00,我希望将时间限制在明天下午2:00之后。我希望有一个24小时的差距。在处理Django表单时,我将如何执行这些复杂的规则?我在博客中读到,我可以在forms.py文件中使用if条件来限制选项,但我想将其保留在客户端。我怎么做到这一点?这是我到目前为止所做的。
我的模板:
<!doctype html>
<html lang="en">
{% load staticfiles %}
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type="text/javascript" src="{% static 'js/datepair.js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery.datepair.js' %}"></script>
</head>
<body>
<form action="." method="post">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit" />
</form>
<script>
var dateToday = new Date();
$( function()
{
$( "#id_pickup_date" ).datepicker({
numberOfMonths: 1,
beforeShowDay: $.datepicker.noWeekends,
showButtonPanel: true,
maxDate: "+2w",
minDate: "+1"
});
});
</script>
</body>
</html>
我的表格:
class SampleForm(forms.Form):
pickup_date = forms.CharField(label='Enter Date', max_length=100)
# pickup_time = forms.CharField(label='Enter Time', max_length=100)
# days = forms.ChoiceField(choices=[(x, x) for x in range(1, 32)])
MY_CHOICES = (
('1', '9:00AM'),
('2', '9:30AM'),
('3', '10:00AM'),
('4', '10:30AM'),
('5', '11:00AM'),
('6', '11:30AM'),
('7', '12:00PM'),
('8', '12:30PM'),
('9', '1:00PM'),
('10', '1:30PM'),
('11', '2:00PM'),
('12', '2:30PM'),
('13', '3:00PM'),
('14', '3:30PM'),
('15', '4:00PM'),
('16', '4:30PM'),
('17', '5:00PM'),
('18', '5:30PM'),
)
my_choice_field = forms.ChoiceField(choices=MY_CHOICES)