Django框架中方法的訪問(wèn)和查找
在 Django 模板中遍歷復(fù)雜數(shù)據(jù)結(jié)構(gòu)的關(guān)鍵是句點(diǎn)字符 (.)。
最好是用幾個(gè)例子來(lái)說(shuō)明一下。 比如,假設(shè)你要向模板傳遞一個(gè) Python 字典。 要通過(guò)字典鍵訪問(wèn)該字典的值,可使用一個(gè)句點(diǎn):
>>> from django.template import Template, Context >>> person = {'name': 'Sally', 'age': '43'} >>> t = Template('{{ person.name }} is {{ person.age }} years old.') >>> c = Context({'person': person}) >>> t.render(c) u'Sally is 43 years old.'
同樣,也可以通過(guò)句點(diǎn)來(lái)訪問(wèn)對(duì)象的屬性。 比方說(shuō), Python 的 datetime.date 對(duì)象有 year 、 month 和 day 幾個(gè)屬性,你同樣可以在模板中使用句點(diǎn)來(lái)訪問(wèn)這些屬性:
>>> from django.template import Template, Context >>> import datetime >>> d = datetime.date(1993, 5, 2) >>> d.year 1993 >>> d.month 5 >>> d.day 2 >>> t = Template('The month is {{ date.month }} and the year is {{ date.year }}.') >>> c = Context({'date': d}) >>> t.render(c) u'The month is 5 and the year is 1993.'
這個(gè)例子使用了一個(gè)自定義的類,演示了通過(guò)實(shí)例變量加一點(diǎn)(dots)來(lái)訪問(wèn)它的屬性,這個(gè)方法適用于任意的對(duì)象。
>>> from django.template import Template, Context >>> class Person(object): ... def __init__(self, first_name, last_name): ... self.first_name, self.last_name = first_name, last_name >>> t = Template('Hello, {{ person.first_name }} {{ person.last_name }}.') >>> c = Context({'person': Person('John', 'Smith')}) >>> t.render(c) u'Hello, John Smith.'
點(diǎn)語(yǔ)法也可以用來(lái)引用對(duì)象的* 方法*。 例如,每個(gè) Python 字符串都有 upper() 和 isdigit() 方法,你在模板中可以使用同樣的句點(diǎn)語(yǔ)法來(lái)調(diào)用它們:
>>> from django.template import Template, Context >>> t = Template('{{ var }} -- {{ var.upper }} -- {{ var.isdigit }}') >>> t.render(Context({'var': 'hello'})) u'hello -- HELLO -- False' >>> t.render(Context({'var': '123'})) u'123 -- 123 -- True'
注意這里調(diào)用方法時(shí)并* 沒(méi)有* 使用圓括號(hào) 而且也無(wú)法給該方法傳遞參數(shù);你只能調(diào)用不需參數(shù)的方法。 (我們將在本章稍后部分解釋該設(shè)計(jì)觀。)
最后,句點(diǎn)也可用于訪問(wèn)列表索引,例如:
>>> from django.template import Template, Context >>> t = Template('Item 2 is {{ items.2 }}.') >>> c = Context({'items': ['apples', 'bananas', 'carrots']}) >>> t.render(c) u'Item 2 is carrots.'
不允許使用負(fù)數(shù)列表索引。 像 {{ items.-1 }} 這樣的模板變量將會(huì)引發(fā)`` TemplateSyntaxError``
Python 列表類型
一點(diǎn)提示: Python的列表是從0開始索引。 第一項(xiàng)的索引是0,第二項(xiàng)的是1,依此類推。
句點(diǎn)查找規(guī)則可概括為: 當(dāng)模板系統(tǒng)在變量名中遇到點(diǎn)時(shí),按照以下順序嘗試進(jìn)行查找:
- 字典類型查找 (比如 foo["bar"] )
- 屬性查找 (比如 foo.bar )
- 方法調(diào)用 (比如 foo.bar() )
- 列表類型索引查找 (比如 foo[bar] )
系統(tǒng)使用找到的第一個(gè)有效類型。 這是一種短路邏輯。
句點(diǎn)查找可以多級(jí)深度嵌套。 例如在下面這個(gè)例子中 {{person.name.upper}} 會(huì)轉(zhuǎn)換成字典類型查找( person['name'] ) 然后是方法調(diào)用( upper() ):
>>> from django.template import Template, Context >>> person = {'name': 'Sally', 'age': '43'} >>> t = Template('{{ person.name.upper }} is {{ person.age }} years old.') >>> c = Context({'person': person}) >>> t.render(c) u'SALLY is 43 years old.'
相關(guān)文章
Python使用Flask框架獲取當(dāng)前查詢參數(shù)的方法
這篇文章主要介紹了Python使用Flask框架獲取當(dāng)前查詢參數(shù)的方法,實(shí)例分析了query_string獲取查詢參數(shù)的技巧,需要的朋友可以參考下2015-03-03一文教你將Visual Studio Code變成Python開發(fā)神器
Visual Studio Code 是一款功能強(qiáng)大、可擴(kuò)展且輕量級(jí)的代碼編輯器,經(jīng)過(guò)多年的發(fā)展,已經(jīng)成為 Python 社區(qū)的首選代碼編輯器之一。本文將為大家介紹一下如何將Visual Studio Code變成Python開發(fā)神器,需要的可以參考一下2022-07-07tensorflow 2.1.0 安裝與實(shí)戰(zhàn)教程(CASIA FACE v5)
這篇文章主要介紹了tensorflow 2.1.0 安裝與實(shí)戰(zhàn)(CASIA FACE v5),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Python異常處理try語(yǔ)句應(yīng)用技巧實(shí)例探究
異常處理在Python中是至關(guān)重要的,try-except是用于捕獲和處理異常的核心機(jī)制之一,本文就帶大家深入了解如何使用try-except,處理各種異常情況2024-01-01Python自定義進(jìn)程池實(shí)例分析【生產(chǎn)者、消費(fèi)者模型問(wèn)題】
這篇文章主要介紹了Python自定義進(jìn)程池,結(jié)合實(shí)例分析了Python使用自定義進(jìn)程池實(shí)現(xiàn)的生產(chǎn)者、消費(fèi)者模型問(wèn)題,需要的朋友可以參考下2016-09-09python獲取指定網(wǎng)頁(yè)上所有超鏈接的方法
這篇文章主要介紹了python獲取指定網(wǎng)頁(yè)上所有超鏈接的方法,涉及Python使用urllib2模塊操作網(wǎng)頁(yè)抓取的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04