Re: Django - technology or magic?

2007-07-24 Thread to_see


Thank you all for the constructive, informative and supportive posts.
I think more time and study will put me right.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Django - technology or magic?

2007-07-24 Thread SH

That's one of the reasons I like django better than rails - Because
there's so little magic. It's all pretty straightforward.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Django - technology or magic?

2007-07-24 Thread Sam Morris

On Mon, 23 Jul 2007 16:57:42 -0700, to_see wrote:

> Snippet #26 solved my problem, essentially in two lines.  I could not
> write those lines for myself now, and I'm not certain I'll ever be able
> to do so.

I'm going to assume it's the Python code itself that is confusing you,
rather than the newforms API...

The lines you're talking about are the body of the constructor in the 
following code:

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

The first line of the constructor is calling the constructor of the
AddSnippetForm's base class, which is forms.Form. This is necessary
so that the work done by the Form class's constructor is still
performed, even though the constructor was overridden by the child
class.

For details, see the definition of the super function in . I *think* (though I am
not certain) that this line could also be written as:

  forms.Form.__init__ (self, *args, **kwargs)

The funny *args and **kwargs arguments simply pass any positional
and keyword arguments that the constructor was called with through
to the constructor of the base class.

The second line is changing the 'choices' property of the 'language' 
field of the form. So far so good -- but what is that weird looking 
expression inside the second set of square brackets on the right hand 
side of the assignment?

Weel, it is a list comprehension. List Comprehensions are just a
concice way of evaluating an expression over each member of a list, and
returning a list of the results. That line could be written out in full 
as:

  x = [('', '--')]
  for lang in Language.objects.all ():
x.append ((lang.id, lang.name))
  self.fields['language'].choices = x

Or by using the map function, as:

  self.fields['language'].choices = [('', '--')] + map (lambda lang: 
(lang.id, lang.name), Language.objects.all ())

More details about List Comprehensions can be found:

 * in the Python tutorial
   
 * in the Python Language Reference
   
 * in PEP 202 (where List Comprehensions were originally defined)
   

When learning any new API/framework along with a language at the
same time, it is often difficult to determine whether something
that you find confusing is a feature of the language, or just an
artifact of the API/framework.

In my experience, once you break down the various little bits
(and know where to find the documentation!), code like this will
start to look a bit less magical and a bit more like, well, regular
code. :)

-- 
Sam Morris
http://robots.org.uk/

PGP key id 1024D/5EA01078
3412 EA18 1277 354B 991B  C869 B219 7FDB 5EA0 1078


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Django - technology or magic?

2007-07-24 Thread Nis Jørgensen

to_see skrev:
> "Any sufficiently advanced technology is indistinguishable from
> magic."
> Arthur C. Clarke, "Profiles of The Future", 1961 (Clarke's third
> law)
>   

> Snippet #26 solved my problem, essentially in two lines.  I could not
> write those lines for myself now, and I'm not certain I'll ever be
> able to do so.
>
> Am I having a fairly normal introduction to a web framework?  I cannot
> see this as technology.  All I see is magic.
>   
FYI: I first looked at the Django source a year and a half ago. The
first time I saw (*args, **kwargs) I ran away screaming, deciding that I
would just try to use the magic, rather than understand it or control it.

Since then I learned some more python, and recognized this as a standard
python idiom.

Yesterday I ran into almost exactly the same problem as you - how to
populate my dropdown at the time of the request, rather than when the
module was loaded. I then went on to write exactly the same code as the
snippet, except for the identifiers and the "blank value" (with a bit of
trial and error).

So learn some python and you will be able to see how the magic works. Or
just lean back and enjoy the show ...

Nis


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Django - technology or magic?

2007-07-24 Thread Thomas Guettler

Am Dienstag, 24. Juli 2007 01:57 schrieb to_see:
> "Any sufficiently advanced technology is indistinguishable from
> magic."
> Arthur C. Clarke, "Profiles of The Future", 1961 (Clarke's third
> law)
>
...
> Am I having a fairly normal introduction to a web framework?  I cannot
> see this as technology.  All I see is magic.

Hi,

If you are new to django and python: It is magic. But if you learn
python, you can find solutions quickly by looking at the source.

Newforms was magic for me first, too. Now I learned how to use this magic.

 Thomas

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Django - technology or magic?

2007-07-24 Thread Jarek Zgoda

to_see napisał(a):

> "Any sufficiently advanced technology is indistinguishable from
> magic."
> Arthur C. Clarke, "Profiles of The Future", 1961 (Clarke's third
> law)
> 
> After getting the tutorial example to work, then making a first-draft
> version of my proposed application, I hit a wall trying to do
> something that seemed both essential and straightforward.  After 3
> days of trying various strategies, I decided that what I needed might
> not be possible.
> 
> I spent about a day browsing all the relevant source code, plus
> anything they used.  I made one last pass through the posts here that
> seemed related, and then hit the Snippets site.
> 
> Snippet #26 solved my problem, essentially in two lines.  I could not
> write those lines for myself now, and I'm not certain I'll ever be
> able to do so.
> 
> Am I having a fairly normal introduction to a web framework?  I cannot
> see this as technology.  All I see is magic.

That's gonna normalize over the days/weeks of framework usage. The more
you'd know about Django internals, the less magic you'd see. In the case
of Django, you have often to look at the framework code to know why
things work in some particular way and this makes Django less "magic" at
every look. ;)

My first encounter with Django was in 0.91 days and this was pure magic.
Then the most of this annoying magic has been removed in 0.95.

-- 
Jarek Zgoda
Skype: jzgoda | GTalk: [EMAIL PROTECTED] | voice: +48228430101

"We read Knuth so you don't have to." (Tim Peters)

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Django - technology or magic?

2007-07-23 Thread Russell Keith-Magee

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
-~--~~~~--~~--~--~---