Widget isn't sticking?

38 views
Skip to first unread message

Lachlan Musicman

unread,
Nov 13, 2013, 1:41:27 AM11/13/13
to django...@googlegroups.com
Hola,

I've set up a CalendarWidget as per the docs
https://docs.djangoproject.com/en/1.6/topics/forms/media/ and can see
correct results in the shell:


forms.py
---------

class CalendarWidget(forms.TextInput):
class Media:
css = {
'all': (settings.STATIC_URL + 'css/datepicker.css',)
}
js = (settings.STATIC_URL + 'js/bootstrap-datepicker.js',)

...

class CarrierWizardForm1(forms.Form):
part_numbers = forms.ModelChoiceField(queryset=PartNumber.objects.all())
expiry_date = forms.DateField(widget=CalendarWidget)


shell
-------

>>> from inventory import forms
>>> w = forms.CalendarWidget()
>>> print(w.media)
<link href="/static/css/datepicker.css" type="text/css" media="all"
rel="stylesheet" />
<script type="text/javascript"
src="/static/js/bootstrap-datepicker.js"></script>
>>> f = forms.CarrierWizardForm1()
>>> print(f.media)
<link href="/static/css/datepicker.css" type="text/css" media="all"
rel="stylesheet" />
<script type="text/javascript"
src="/static/js/bootstrap-datepicker.js"></script>


But in the browser I'm not seeing the new widget.

What am I doing wrong? Do I have to explicitly pull the assets into
the template in question?
Am I meant to be explicitly referring to the widget in the template?

I note that using the built in SelectDateWidget works fine:

from django.forms.extras.widgets import SelectDateWidget
class CarrierWizardForm1(forms.Form):
part_numbers = forms.ModelChoiceField(queryset=PartNumber.objects.all())
#expiry_date = forms.DateField(widget=CalendarWidget)
expiry_date = forms.DateField(widget=SelectDateWidget)


Cheers
L.


--
From this perspective it is natural that anarchism be marked by
spontaneity, differentiation, and experimentation that it be marked by
an expressed affinity with chaos, if chaos is understood to be what
lies outside or beyond the dominant game or system. Because of the
resistance to definition and categorisation, the anarchist principle
has been variously interpreted as, rather than an articulated
position, “a moral attitude, an emotional climate, or even a mood”.
This mood hangs in dramatic tension between utopian hope or dystopian
nihilism...
-----
http://zuihitsu.org/godspeed-you-black-emperor-and-the-politics-of-chaos

Jason Arnst-Goodrich

unread,
Nov 13, 2013, 8:37:16 PM11/13/13
to django...@googlegroups.com
Try:

expiry_date = forms.DateField(widget=CalendarWidget())

Lachlan Musicman

unread,
Nov 13, 2013, 9:31:40 PM11/13/13
to django...@googlegroups.com
Nothing, unfortunately.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/24f84c8e-f8bc-4dc0-a519-b1c3d4740d9b%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Jason Arnst-Goodrich

unread,
Nov 14, 2013, 12:09:22 AM11/14/13
to django...@googlegroups.com
Actually you're using something I'd probably not even bother using a widget for.

Really, you probably just want to include the appropriate CSS and JS in your template (although you can use the widget class as well - the key here is that the CSS and JS need to be on your page somewhere).

You then need to trigger the date selector.

Docs show 
  1. $('.datepicker').datepicker()

Which would find all inputs of class 'datepicker'

So we need to set the class on your input.

expiry_date = forms.DateField(widget=CalendarWidget(attrs={'class': 'datepicker'}))

note you can just use the TextInput widget if you're including the CSS/JS in your template. That tends to be what I do unless I'm creating a full featured widget which is pretty rare.

expiry_date = forms.DateField(widget=TextInput(attrs={'class': 'datepicker'}))
Reply all
Reply to author
Forward
0 new messages