2👍
✅
Here is an example.
Form for validating date input:
class PickDateForm(forms.Form):
date = forms.DateField(widget=forms.DateInput(attrs={'class': 'any_custom_class_name'}))
View:
def query_by_month(request):
if request.method == 'POST':
form = PickDateForm(request.POST)
if form.is_valid():
selected_date= form.cleaned_data['date'] # 'selected_date' is a datetime.date object
photos = Photo.objects.filter(uploaded_time__year=selected_date.year,
uploaded_time__month=selected_date.month)
return HttpResponse('Photos: ' + repr(photos))
else:
return HttpResponse('Invalid form')
else:
form = PickDateForm()
return render(request, 'pickdate.html', {'form': form})
The HTML (using jQuery datepicker):
<!DOCTYPE html>
<html>
<head>
<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.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script>
$(function() {
$( ".any_custom_class_name" ).datepicker();
});
</script>
</head>
<body>
<form action="/pick_date/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit" />
</form>
</body>
</html>
👤ZZY
Source:stackexchange.com