On 7/24/07, to_see <[EMAIL PROTECTED]> wrote:

> Am I having a fairly normal introduction to a web framework?  I cannot
> see this as technology.  All I see is magic.

You should note that when Clarke made that comment, he was referring
to the fact that if you don't understand the fundamentals of a given
area of study, anything that happens in that field will _appear_ to be
magic. It isn't _actually_ magic - you just need to improve your
understanding of the relevant fundamentals if you want to understand
what is going on.

You seem to expect that by finishing the tutorial example you will be
immediately able to develop the greatest web application in the world.
To be quite frank, this seems more than a little unreasonable. It is
also unreasonable to believe that your capabilities won' t improve
with experience.

Django has a very large user base, and a very helpful mailing list.
Apparently, you need to have dynamic choices on a form field. You have
found an example that shows how to do what you want to do. Django's
documentation is generally pretty good, although I will be the first
to admit that the newforms documentation is a work in progress and
could be improved. However, rather than going on a rant about how
everything seems like magic, how about asking a direct question: "Can
anyone explain exactly how snippet 26 works?".

In this case, the answer is fairly simple: A Form definition describes
the fields you want on the form:

class MyForm(Form):
   name = forms.CharField(max_length=20)
   language = forms.ChoiceField(choices=[('', '----------')] +
[(lang.id, lang.name) for lang in Language.objects.all()],
widget=forms.Select(attrs=attrs_dict))

Now, this definition will get turned into a form, but the 'choices'
made available to the language field will be determined at the time
the Form is parsed by Python, not dynamically when you use the form.
If you want a the choices list to be dynamically populated with
current data from the database, you need to defer the evaluation of
the list of choices until the form is actually used. You do this by
overriding the constructor for the form to tweak the choices option
for the language form field:

class MyForm(Form):
   def __init__(self, *args, **kwargs):
       super(MyForm, self).__init__(*args, **kwargs)
       self.fields['language'].choices = [('', '----------')] +
[(lang.id, lang.name) for lang in Language.objects.all()]

   name = forms.CharField(max_length=20)
   language = forms.ChoiceField(choices=(),
widget=forms.Select(attrs=attrs_dict))

It's not magic - it just requires an understanding of a field that you
are yet to master.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to