Reputation: 327
i wanna have a textbox in my web page with an image next to it. when user clicks on image, it shows the calander that help user choose the date (popup calendar). i've done it in raw html. but now i wanna use django forms(although i don't have any models) so i searched and found out that i have to write my own widget in forms.py. s.th like:
class CalendarWidget(forms.TextInput):
css = ('/media/css/main.css', '/media/css/js-cal.css')
js = ('/media/js/js-cal.min.js')
i have a class backupForm
in forms.py
which contains all form fields.
my question is: to use the above CalendarWidget
, should i define a field in backupForm
class like this?:
date1 = forms.CharField(widget=CalendarWidget)
how can i bind an image to this textbox and force user that only input his date from this calendar?
Upvotes: 0
Views: 888
Reputation: 23871
You could use the form field of the input field(textbox) and apply CalendatWidget
as the widget of the field. The widget takes the responsibility of creating/binding the image which works as a button.
Django Admin use a AdminDateWidget
on form field of model.DateField
automatically, despite the modelform part, you could check it in django/contrib/admin/widgets.py
. Check init()
function inside django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js
to know how does Django create/bind the calendar button to one input field.
You could even use AdminDateWidget
in a front page, as long as the page loads required CSS/JS resources and has certain attribute set....
Upvotes: 0