Start with the very useful tutorial at
http://www.djangoproject.com/documentation/forms/ - in general it's
very helpful, but there are some small gaps in the documentation, which
would certainly have helped me
#1: I'd emphasize that this document
(http://www.djangoproject.com/documentation/forms/), will help
djangonauts get a lot of the adminy goodness into their own apps. Big
win to the django team.
#2: The date and time javascript widgets are what I wanted most of all.
It would be handy if the example model included a DateTime field.
Here's what I have;
startdate = meta.DateTimeField(db_column='start_date') # there
were too many underscores, I wanted to keep it simple!!
This is represented in the form template like this;
<div class="form-row">
<label for="id_startdate_date">Start Date:</label>
<p class="datetime">
{{ form.startdate_date }} {{ form.startdate_time }}
<br/>
</p>
{% if form.startdate.errors %}*** {{
form.startdate.errors|join:", " }}{% endif %} <br/>
</div>
The big thing here is that the field 'startdate' is expanded out in the
form, _date and _time are made available (this may have been where I
went wrong, my initial attempt had {{ form.start_date }}, but I never
saw any content in the form).
#3: There seems to be an error in the code for loading up an edit_form.
The example document has this;
if request.POST:
new_data = request.POST.copy()
errors = manipulator.get_validation_errors(new_data)
if not errors:
manipulator.do_html2python(new_data)
manipulator.save(new_data)
# Do a post-after-redirect so that reload works, etc.
return HttpResponseRedirect("/places/edit/%i/" % place.id)
else:
errors = {}
# This makes sure the form accurate represents the fields of
the place.
new_data = place.__dict__
*no way* could I get this to work, I had to do this instead
if request.POST:
new_data = request.POST.copy()
errors = manipulator.get_validation_errors(new_data)
if not errors:
manipulator.do_html2python(new_data)
manipulator.save(new_data)
# Do a post-after-redirect so that reload works, etc.
return HttpResponseRedirect("/resources/edit/%i/" %
resource.id)
else:
errors = {}
# This makes sure the form accurate represents the fields of
the place.
#new_data = resource.__dict__
new_data = manipulator.flatten_data()
Note that
new_data = resource.__dict__
is replaced with
new_data = manipulator.flatten_data()
Ok, nearly there, but could I get the clock and calendar widgets? Nope
- I used Xylescope to check on the CSS, I used alerts in the Javascript
etc. etc. and then I found it was stopping on gettext... Some
additional digging provided this jewel; i18n.txt in the docs.
Add this;
(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', {'packages':
'django.conf'}),
to your urls.py for your application, make sure that your header HTML
looks *something* like this;
<script type="text/javascript" src="../../../jsi18n/"></script>
<script type="text/javascript" src="/media/js/core.js"></script><script
type="text/javascript"
src="/media/js/admin/RelatedObjectLookups.js"></script><script
type="text/javascript" src="/media/js/calendar.js"></script><script
type="text/javascript"
src="/media/js/admin/DateTimeShortcuts.js"></script>
And Bob's your Uncle, Fannys your Aunt (ie the javascript widgets
work).
Hope this helps ;)
Cheers,
Tone
Come to think of it, I may try diffing against the original docs and
submitting it as a ticket (first attempt at that though...)
Cheers,
Tone