jQuery 如何在 Django 中使用 Datepicker
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20700185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Datepicker in django
提问by Bishnu Bhattarai
I want to implement a django form with datepicker. I made my forms.py
我想用 datepicker 实现一个 django 表单。我做了我的forms.py
from django import forms
class DateRangeForm(forms.Form):
start_date = forms.DateField(widget=forms.TextInput(attrs=
{
'class':'datepicker'
}))
end_date = forms.DateField(widget=forms.TextInput(attrs=
{
'class':'datepicker'
}))
and views.py
和 views.py
if request.method == "POST":
f = DateRangeForm(request.POST)
if f.is_valid():
c = f.save(commit = False)
c.end_date = timezone.now()
c.save()
else:
f = DateRangeForm()
args = {}
args.update(csrf(request))
args['form'] = f
return render(request, 'trial_balance.html', {
'form': f
})
balance.html
余额.html
<div>
<form action="" method="POST"> {% csrf_token %}
Start Date:{{ form.start_date }} End Date:{{ form.end_date }}<br/>
<input type = "submit" name = "submit" value = "See Results">
</form>
</div>
And still there is no datepicker in my input box of that form. I also tried with including my files link in the script as in my balance.html
我的那个表单的输入框中仍然没有日期选择器。我还尝试在脚本中包含我的文件链接,如我的 balance.html
<script src="{{ STATIC_URL }}js/jquery-1.3.2.min.js"></script>
still the datepicker is not working. But when including jquery in my html file, it also makes not to work jquery-treetable which I have implemented in my html file.
日期选择器仍然无法正常工作。但是当在我的 html 文件中包含 jquery 时,它也会使我在我的 html 文件中实现的 jquery-treetable 不起作用。
How to make the datepicker work ?
如何使日期选择器工作?
回答by niekas
You can use forms.DateInput()
widget, instead of forms.TextInput()
:
您可以使用forms.DateInput()
小部件,而不是forms.TextInput()
:
from functools import partial
DateInput = partial(forms.DateInput, {'class': 'datepicker'})
class DateRangeForm(forms.Form):
start_date = forms.DateField(widget=DateInput())
end_date = forms.DateField(widget=DateInput())
To make JQuery Datepickerwork, you have to initialise it:
要使JQuery Datepicker工作,您必须对其进行初始化:
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$('.datepicker').datepicker();
});
</script>