ChoiceField Being Unpatriotic

2010-04-17 Thread amyhalf
Hi, all!

For the life of me I can't get anything to show up in a drop-down.

Here's the model:

class States(models.Model):
id = models.IntegerField(unique = True, primary_key = True)
abbrev = models.CharField(max_length = 2, blank = False)
long_form = models.CharField(max_length = 64, blank = False)

def __str__(self):
return "".join( [ self.abbrev, ' (', self.long_form, ')' 
])

def __unicode__(self):
return self.long_form

class Meta:
db_table = u'states'

Here's the form:

class AddressForm(forms.Form):
first_and_last_name = forms.CharField(128, 1)
street1 = forms.CharField(128, 1)
street2 = forms.CharField(128, 1)
city = forms.CharField(64, 1)
state = forms.ChoiceField()

Here's how I'm trying to populate the states:

form = AddressForm()
form.state = ModelChoiceField(States.objects.all())
return render_to_response('subscribe_step2.html', { 'form' : form,
'user_account' :  user_account })

Now, I can successfully do this:  return
HttpResponse(States.objects.all())and get an (albeit ugly) list of
States in my browser so I definitely know it's working.

Here's the snippet from the template:


State
{{ form.state }}


Or, while debugging, if I go:  form.as_p()   it shows it correctly
made the  tag but nothing else.

I'm sure I'm doing one little thing wrong and someone can fix it in
two minutes.  Help!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: ChoiceField Being Unpatriotic

2010-04-18 Thread amyhalf
Hi, thanks for the reply!

I changed it to what you suggested but it's mad about something else,
now:

TemplateSyntaxError
Caught an exception while rendering: 'ModelChoiceField' object has no
attribute 'all'

It's mad about how render it:

{{ form.state }}

I didn't read here (http://docs.djangoproject.com/en/dev/ref/forms/
fields/#modelchoicefield) that I need to do anything wacky but I guess
my model has to override or define an "all" method?

Thanks in advance!


On Apr 17, 11:34 am, "ge...@aquarianhouse.com"
 wrote:
> try with:
>
> class AddressForm(forms.Form):
>         first_and_last_name = forms.CharField(128, 1)
>         street1 = forms.CharField(128, 1)
>         street2 = forms.CharField(128, 1)
>         city = forms.CharField(64, 1)
>         state = forms.ModelChoiceField(queryset=States.objects.all())
>
> form = AddressForm()
> return render_to_response('subscribe_step2.html',
> { 'form' : form,
> 'user_account' :  user_account })
>
> On Apr 17, 5:34 pm, amyhalf  wrote:
>
>
>
>
>
> > Hi, all!
>
> > For the life of me I can't get anything to show up in a drop-down.
>
> > Here's the model:
>
> > class States(models.Model):
> >         id = models.IntegerField(unique = True, primary_key = True)
> >         abbrev = models.CharField(max_length = 2, blank = False)
> >         long_form = models.CharField(max_length = 64, blank = False)
>
> >         def __str__(self):
> >                 return "".join( [ self.abbrev, ' (', self.long_form, ') > />' ])
>
> >         def __unicode__(self):
> >                 return self.long_form
>
> >         class Meta:
> >                 db_table = u'states'
>
> > Here's the form:
>
> > class AddressForm(forms.Form):
> >         first_and_last_name = forms.CharField(128, 1)
> >         street1 = forms.CharField(128, 1)
> >         street2 = forms.CharField(128, 1)
> >         city = forms.CharField(64, 1)
> >         state = forms.ChoiceField()
>
> > Here's how I'm trying to populate the states:
>
> > form = AddressForm()
> > form.state = ModelChoiceField(States.objects.all())
> > return render_to_response('subscribe_step2.html', { 'form' : form,
> > 'user_account' :  user_account })
>
> > Now, I can successfully do this:  return
> > HttpResponse(States.objects.all())    and get an (albeit ugly) list of
> > States in my browser so I definitely know it's working.
>
> > Here's the snippet from the template:
>
> >         
> >                 State
> >                 {{ form.state }}
> >         
>
> > Or, while debugging, if I go:  form.as_p()   it shows it correctly
> > made the  tag but nothing else.
>
> > I'm sure I'm doing one little thing wrong and someone can fix it in
> > two minutes.  Help!
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Django users" group.
> > To post to this group, send email to django-us...@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > django-users+unsubscr...@googlegroups.com.
> > For more options, visit this group 
> > athttp://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Please help me make a SELECT box!

2010-04-19 Thread amyhalf
For the life of me I can't get Django to render even the simplest of
HTML  boxes.

Here's a recent example that I have which I cannot get to work.

This is the form:

class CreditCardForm(forms.Form):
fullname = forms.CharField(128, 1, required = True)
mm_expiry = forms.ChoiceField(required = True)

In the view, I do the following before sending it to the template:

form = CreditCardForm()
form.mm_expiry.choices = (('1','1'), ('2','2'))

Here's how I print it in the template:

Expiration date:
{{ form.mm_expiry }}

It does make the  tag but will not populate the individual
items.

Help!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.