I see what you are saying, but I was trying to do something
a little
more complicated. I wasn't trying to validate any data, but
rather
load choices into a select box based on information from the
POST. I
have 2 select boxes in a form where one has info regarding
all the
countries in the world. Onces the user selects a country, I
populate
another select box with all the states for that country. I
decided to
do most of this with javascript and got it working that
way.
But then I ran into another problem. If the page has an
error and is
reloaded then the states aren't populated in the select box
again. I
had to update the constructor in my form and update those
select boxes
manually with the correct choices. What I didn't know
previously was
a form constructor always has a self.data variable once a
request is
passed in. Here is my code if anyone is interested:
from main_site.static.models import StaticCountries,
StaticRegions
def __init__(self, *args, **kwargs):
super(CustomerAdd, self).__init__( *args, **kwargs)
#checking if this is post information
if (self.is_bound == True):
#we have to check if the user selected a country
in
mailing/shipping/or billing select boxes
#...if they did, then we have to populate the
state select
box
# with the corresponding states for that
country
if (self.data.get('mailing_country') !=
"None"):
self.fields['mailing_state'].choices =
([x.region,
x.region] for x in
StaticRegions.objects.filter(country_code=self.data.get('mai
ling_country')).order_by("region"))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-users googlegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribe googlegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
|