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
balance.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
在我的输入框中仍然没有datepicker。我还尝试在脚本中添加文件链接,就像在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.
但datepicker仍然没有工作。但是,当在html文件中包含jquery时,它也使我不能使用我在html文件中实现的jquery-treetable。
How to make the datepicker work ?
如何使datepicker工作?
1 个解决方案
#1
30
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 Datepicker work, 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>
#1
30
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 Datepicker work, 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>