Python论坛  - 讨论区

标题:[python-chinese] 转发django论坛的一个帖子与大家共勉

2007年07月24日 星期二 16:19

Li Qingfeng liqfemail在gmail.com
星期二 七月 24 16:19:27 HKT 2007

ÓеÄʱºòÕæµÄºÍÔ­Ìù×÷ÕßÓÐͬ¸Ð£¬ÏÖÔÚÓ¦Óõļ¼Êõ×ÜÊÇÌ«¸´ÔÓ£¬¸Õ¿ªÊ¼Ñ§µÄʱºò¸Ð¾õÏñ"ħÊõ"Ò»Ñù¡£
ÓÐʱºòÒ»¸öСÎÊÌâÀ§ÈÅÄã°ëÌ죬×îºó¼ÓÉÏÒ»Á½ÐдúÂë¾ÍÉñÆæµÄ½â¾öÁË¡£¸ü¶àµÄÀ§ÈÅÔ´×ÔÓÚ¶ÔÕûÌå¿ò¼ÜºÍÁ÷³ÌµÄ²»Á˽⣬һͷÎíË®¡£
Õâ¸ö»ØÌû¸øÁËÎҺܴóµÄ¼¤Àø£¬Ò²¸øÈçºÎÃæ¶ÔÕâÒ»À§ÈÅÌṩÁËÖ¸µ¼·½Õ룬ÏÖÔÚת¸ø´ó¼Ò¹²Ãã¡£

Ô­ÌùÈçÏ£º
"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.

ÒÔÏÂÊÇ»ØÌù£º
> 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 %-)

ÁíÒ»¸ö»ØÌû£º
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
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070724/d4ebfe45/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月24日 星期二 16:22

beck917 beck917在gmail.com
星期二 七月 24 16:22:53 HKT 2007

Ó¢ÓïÔÝʱû¿Õ¿´¡£¡£¡£ºÃÐÄÈ˸ø·­ÒëÏ¡£¡£

 

  _____  

·¢¼þÈË: python-chinese-bounces在lists.python.cn
[mailto:python-chinese-bounces在lists.python.cn] ´ú±í Li Qingfeng
·¢ËÍʱ¼ä: 2007Äê7ÔÂ24ÈÕ 16:19
ÊÕ¼þÈË: python-chinese在lists.python.cn
Ö÷Ìâ: [python-chinese] ת·¢djangoÂÛ̳µÄÒ»¸öÌû×ÓÓë´ó¼Ò¹²Ãã

 

ÓеÄʱºòÕæµÄºÍÔ­Ìù×÷ÕßÓÐͬ¸Ð£¬ÏÖÔÚÓ¦Óõļ¼Êõ×ÜÊÇÌ«¸´ÔÓ£¬¸Õ¿ªÊ¼Ñ§µÄʱºò¸Ð¾õÏñ
"ħÊõ"Ò»Ñù¡£
ÓÐʱºòÒ»¸öСÎÊÌâÀ§ÈÅÄã°ëÌ죬×îºó¼ÓÉÏÒ»Á½ÐдúÂë¾ÍÉñÆæµÄ½â¾öÁË¡£¸ü¶àµÄÀ§ÈÅÔ´×Ô
ÓÚ¶ÔÕûÌå¿ò¼ÜºÍÁ÷³ÌµÄ²»Á˽⣬һͷÎíË®¡£ 
Õâ¸ö»ØÌû¸øÁËÎҺܴóµÄ¼¤Àø£¬Ò²¸øÈçºÎÃæ¶ÔÕâÒ»À§ÈÅÌṩÁËÖ¸µ¼·½Õ룬ÏÖÔÚת¸ø´ó¼Ò¹²
Ãã¡£

Ô­ÌùÈçÏ£º
"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. 

ÒÔÏÂÊÇ»ØÌù£º

> 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 <http://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 %-) 

ÁíÒ»¸ö»ØÌû£º
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 

-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070724/de0e794a/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]
cyt

cyt

2007年07月27日 星期五 18:42

yuting cui yutingcui在gmail.com
星期五 七月 27 18:42:05 HKT 2007

我觉得问题出在那家伙对python不熟悉,而不是django……

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年07月28日 星期六 08:49

Atim bkkkd在foxmail.com
星期六 七月 28 08:49:50 HKT 2007

  好像昨晚开始收不了列表

------------------				 
Atim
2007-07-28

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号