Revision [1220]

Last edited on 2010-04-11 16:24:50 by KlenwellAdmin
Additions:
{{redirect page="AppengineForms"}}
Deletions:
====Google App Engine Forms====
I'm learning the djangoforms API that GAE uses. I'm finding the django documentation on this subject a bit incomplete. Below are some problems I've encountered.

return to [[CategoryGoogleAppEngine Google App Engine home]]

===Widget Overriding===
==Problem==
By default when you define a model with choices and create a modelform, the widget for that model (the html output) will be a select box. But what if you want the user to be able to select more than one choice. Then you'd want something like a list of checkboxes. The django API offers such a widget. But getting it to function was a bit of a challenge.

==Solution==
In my case, I was trying to figure this out in the context of a wizard I was putting together. (I know djangoforms offer its own wizard framework, but the bundled Google version doesn't support it yet.) So my code a little more complicated, but I've simplified it below.

This is what worked:
%%(python)
# set choice list: notice that we require a tuple of tuples containing both value and label items
# the google examples gave me the impression you could use a 1-D sequence
CHOICES_PICKTWO = (
# value, label
('fast',"fast"),
('cheap',"cheap"),
('works',"works")
)

# our class with the picktwo field using choice list above
class DemoModel(db.Model):
email = db.EmailProperty(required=True)
url = db.LinkProperty(required=True)
picktwo = db.StringProperty(required=True, choices=CHOICES_PICKTWO)
message = db.StringProperty(required=True)
created = db.DateTimeProperty(auto_now_add=True)
modified = db.DateTimeProperty(auto_now=True)


# extending charField with own class that overrides clean method (used for validation) so that
# it requires two items be picked from list
class PickTwoField(forms.CharField):
def clean(self, value):
if len(value) != 2:
raise forms.ValidationError('Please select two (2) choices below')
return value


# modelform class
class DemoModelForm(djangoforms.ModelForm):
# NOTE: we have to explicitly pass our choice list as a key argument to our widget constructor
# Some examples I saw gave me the impression that you could just set widget to a value
# like so: widget=forms.CheckboxSelectMultiple
picktwo = PickTwoField(widget=forms.CheckboxSelectMultiple(choices=CHOICES_PICKTWO))

class Meta:
model = DemoModel
fields = ('email', 'picktwo')


html_form = """
<form method="POST" action="%s">
<table>
%s
</table>
<input type="submit" value="continue" />
</form>
""" % ( action, DemoModelForm() )
%%

==Demo==
I haven't put up a live demo yet. I'll link it once I've uploaded it.


Revision [913]

Edited on 2008-05-31 17:15:03 by KlenwellAdmin
Additions:
# extending charField with own class that overrides clean method (used for validation) so that
# it requires two items be picked from list
class PickTwoField(forms.CharField):
def clean(self, value):
if len(value) != 2:
raise forms.ValidationError('Please select two (2) choices below')
return value
picktwo = PickTwoField(widget=forms.CheckboxSelectMultiple(choices=CHOICES_PICKTWO))
Deletions:
picktwo = forms.CharField(widget=forms.CheckboxSelectMultiple(choices=CHOICES_PICKTWO))


Revision [911]

Edited on 2008-05-31 10:36:55 by KlenwellAdmin
Additions:
('fast',"fast"),
('cheap',"cheap"),
('works',"works")
Deletions:
('FAST',"fast"),
('CHEAP',"cheap"),
('WORKS',"works")


Revision [910]

Edited on 2008-05-31 10:36:30 by KlenwellAdmin
Additions:
===Widget Overriding===
Deletions:
===Radio / Checkbox Lists===


Revision [909]

Edited on 2008-05-31 10:35:35 by KlenwellAdmin
Additions:
return to [[CategoryGoogleAppEngine Google App Engine home]]
# set choice list: notice that we require a tuple of tuples containing both value and label items
# the google examples gave me the impression you could use a 1-D sequence
# NOTE: we have to explicitly pass our choice list as a key argument to our widget constructor
model = DemoModel
Deletions:
# set choice list: notice that we require a tuple of tuples containg both value and label items
# the google examples gave me the impress you could use a 1-D sequence
# NOTE: we have to explicit pass our choice list as a key argument to our widget constructor
model = DemoMode


Revision [908]

The oldest known version of this page was created on 2008-05-31 10:26:01 by KlenwellAdmin
Valid XHTML 1.0 TransitionalValid CSSWikkaWiki