Fwd: Re: Inputting Date/Time in a Django Form

232 views
Skip to first unread message

Tim Sawyer

unread,
Jul 2, 2008, 4:52:21 PM7/2/08
to django...@googlegroups.com
Sorry, I don't understand where I have to put the attrs bit.

Can you give me an example?

delivery_datetime = forms.SplitDateTimeField(attrs={'class':'vDateField'})
doesn't work, is that what you meant?

Thanks,

Tim.

On Sunday 29 Jun 2008, TiNo wrote:
> add:
>
> <script src="{% admin_media_prefix %}js/core.js" type="text/javascript"
>
> ></script>
>
> <script src="{% admin_media_prefix %}js/admin/Calendar.js"
> type="text/javascript" ></script>
> <script src="{% admin_media_prefix %}js/admin/DateTimeShortcuts.js"
> type="text/javascript" ></script>
>
> to your template, use SplitDateTimeField and pass the fields:
>
> attrs={'class':'vDateField'}
>
> That way you have the same date and time input as the admin interface.
>
> TiNo
>
> On Sun, Jun 29, 2008 at 10:46 PM, Tim Sawyer <list....@calidris.co.uk>
>
> wrote:
> > Hi Folks,
> >
> > What's the simplest approach for me to have a form that makes it easy for
> > the
> > user to enter a Date/Time combination?
> >
> > I have my form class specified (the field in question is a
> > forms.DateTimeField()), but I need to make it easy for users of the site
> > to enter a date/time pair for the field.
> >
> > Is there a simple way to do this? I'm using jQuery for javascript on the
> > site
> > if that's relevant.
> >
> > Thanks,
> >
> > Tim.
>
>


-------------------------------------------------------

TiNo

unread,
Jul 2, 2008, 4:57:44 PM7/2/08
to django...@googlegroups.com
Sorry, I was a little too quick. I used it like this:

date = forms.DateField(label=_("Date"),widget=forms.DateTimeInput(attrs={'class':'vDateField'}))

I am not sure on how to do this on a SplitDateTimeField though.

Tim Sawyer

unread,
Jul 2, 2008, 5:03:15 PM7/2/08
to django...@googlegroups.com
Thanks.

I've got the class in the html but not the buttons for the date or time popup.
I also have two date popups and I think I should have one that has the class
vTimeField not vDateField.

collection_datetime =
forms.SplitDateTimeField(widget=forms.SplitDateTimeWidget({'class':'vDateField
required'}))

in my form is generating:

<tr><th><label for="id_collection_datetime_0">Collection
datetime:</label></th><td><input id="id_collection_datetime_0" type="text"
class="vDateField required" name="collection_datetime_0" /><input
id="id_collection_datetime_1" type="text" class="vDateField required"
name="collection_datetime_1" /></td></tr>

in my html.

Any ideas?

Cheers,

Tim.

TiNo

unread,
Jul 2, 2008, 5:13:55 PM7/2/08
to django...@googlegroups.com
Have a look at the source (widgets.py in newforms):

class SplitDateTimeWidget(MultiWidget):
    """
    A Widget that splits datetime input into two <input type="text"> boxes.
    """
    def __init__(self, attrs=None):
        widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs))
        super(SplitDateTimeWidget, self).__init__(widgets, attrs)

So maybe:

collection_datetime =
forms.SplitDateTimeField(
widget=forms.SplitDateTimeWidget(
widgets=(TextInput({'class':'vDateField required'}),
TextInput({'class':'vTimeField'}))))

will work?

Tim Sawyer

unread,
Jul 2, 2008, 5:42:47 PM7/2/08
to django...@googlegroups.com
Sorry, still not working. I get:

__init__() got an unexpected keyword argument 'widgets'

Is that because widgets isn't a parameter to __init__ ? (I'm still getting to
grips with python, it's so much more expressive than the Java I'm used to!)

My code is:

collection_datetime = forms.SplitDateTimeField(widget=

forms.SplitDateTimeWidget(widgets=
(

forms.TextInput({'class':'vDateField required'}),

forms.TextInput({'class':'vTimeField required'})
)
)
)

ta,

Tim.

> >Field required'}))

TiNo

unread,
Jul 3, 2008, 6:07:36 AM7/3/08
to django...@googlegroups.com
Ah yeah.
Maybe:
 
collection_datetime = forms.SplitDateTimeField(widget=forms.SplitDateTimeWidget())
collection_datetime.widget.widgets = (input1.. , input2..)
would do the trick?
 
I don't have a development env here so I am only guessing.
Anybody else has suggestions on this? Because it looks a little hacky...
 
TiNo

 

Tim Sawyer

unread,
Jul 4, 2008, 3:55:00 PM7/4/08
to django...@googlegroups.com
Thanks anyway, I got it to work using a jQuery date popup and manually writing
the forms html.

Cheers,

Tim.

Reply all
Reply to author
Forward
0 new messages