tags:

views:

519

answers:

3

Hi,django newbie here. I have an attempt of a registration page.

It consists of text fields (name, username, password, etc) and a DateField (birthday).

I'm trying to use a forms.DateTimeInput widget for the DateField, but when the page is rendered, nothing happens. Am i doing something wrong? Does this have something im overlooking? It seems pretty simple, but i cant get it to work.

This is my form

class RegisterForm(forms.Form):
    username  = forms.CharField(max_length= 50)
    password = forms.CharField(label=u'Password',widget = forms.PasswordInput(render_value=False))#password

    birthday = forms.DateField(required=False,initial=datetime.date.today,widget = forms.DateTimeInput())#25 Oct 2006

And my very simple HTML

<form action="" method = "POST">
{{ form.as_p}}
<input type='Submit' value='Register'/>
</form>
+2  A: 

You shouldn't use a DateTimeInput with a DateField - either use a DateInput, or a DateTimeField.

However, I suspect what you mean by 'nothing happens' is that the links for the calendar date selector don't appear. These aren't part of the standard forms framework, and the documentation doesn't suggest that they do appear with a Date/DateTimeInput. They're part of the admin application, so if you need them in your own app you will need to include the javascript files explicitly.

The easiest way to do this is probably to use AdminDateWidget from django.contrib.admin.widgets, rather than the DateInput. You will also need to include the jsi18n script in your template:

<script type="text/javascript" src="/admin/jsi18n/"></script>
Daniel Roseman
I'm really confused about where Django is with datetime widgets. Docs imply an update in 1.1 but I'm not clear what it is. If I have a form like this:<pre>class EventForm(forms.ModelForm): summary = forms.CharField(label="Event Name", widget=forms.TextInput(attrs={'size':'40'})) dtstart = forms.DateTimeField(widget=AdminDateWidget()) dtend = forms.DateTimeField(widget=AdminDateWidget)</pre>The javascript is loaded, but not calendar icon appears in the form. Would love some clarity around this as I keep having to go back to jQuery.
+3  A: 

calendar date selector is not part of forms framework.
You can however use jQuery date selector widget

<script type="text/javascript" src="{{ MEDIA_URL }}js/jquery-min.js"></script> 
<!-- give correct location for jquery.js -->
<script type="text/javascript" src="{{ MEDIA_URL }}js/jquery-ui-min.js"></script> 
<!-- give correct location for jquery-ui.js -->
<script type="text/javascript">
   jQuery(function() {
       jQuery("#id_birthday").datepicker({ dateFormat: 'yy-mm-dd' });
   });
</script>
<form method="post">
    {{ form.as_p }}
    <input type="submit" value="Create Phase" />
</form>

On clicking/selecting the birthday textbox, it will show a calendar selector widget
Please give correct location of your jquery.js and jquery-ui.js

Rajani Karuturi