You want some of that 'adminy' goodness in your own apps?

28 views
Skip to first unread message

tonemcd

unread,
Feb 1, 2006, 2:16:30 AM2/1/06
to Django users
Here's what I've found...

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

Bryan Murdock

unread,
Feb 1, 2006, 3:33:44 PM2/1/06
to django...@googlegroups.com
That's cool. Did you put this on the wiki?

http://code.djangoproject.com/

Bryan

tonemcd

unread,
Feb 2, 2006, 12:27:01 AM2/2/06
to Django users
Hadn't thought of that Brian, I'll have a go at putting something a bit
more sensible into the Wiki (I was in a hurry to go to a meeting and
wanted to get the content online somewhere before it left me
completely!).

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

Reply all
Reply to author
Forward
0 new messages