【发布时间】:2014-04-27 19:11:09
【问题描述】:
我在Djangobook 中遇到了这个code。我无法理解这是什么意思。
urlpatterns=pattern('django.views.generic',
url(r'^$','simple.direct_to_template',
kwargs={
'template':'index.html',
'extra_content':{'item_list':lambda:Item.objects.all()}
}
)
并且在模板中,简单的迭代来显示项目
{% for item in item_list %}
上述网址中lambda的用途是什么?
P.S:我知道 lambda 在 python (anonymous function) 中的工作原理。但我想知道它在这里的用途。为什么不使用'extra_content':{'item_list':Item.objects.all()},因为无论如何都会产生iterable。
编辑:
如果我有
info_dict={'queryset':Item.objects.all()}
并将这个dict 传递给extra_content
'extra_content':{'item_list':queryset}
会不会和lambda一样(我在djangobook里见过这种例子}
【问题讨论】:
标签: django lambda django-urls django-generic-views url-pattern