自定義模板標(biāo)簽,過(guò)濾器。英文翻譯是Customtemplatetagsandfilters。customfilter自定義過(guò)濾器今天不在我的記錄范圍之內(nèi),以后用到再看官方文檔也不遲。
**問(wèn)題1:**customtemplatetags到底長(zhǎng)啥樣?
customtemplatetags—github
Manytemplatetagstakeanumberofarguments–stringsortemplatevariables–andreturnaresultafterdoingsomeprocessingbasedsolelyontheinputargumentsandsomeexternalinformation.Forexample,acurrent_timetagmightacceptaformatstringandreturnthetimeasastringformattedaccordingly.
Toeasethecreationofthesetypesoftags,Djangoprovidesahelperfunction,simple_tag.Thisfunction,whichisamethodofdjango.template.Library,takesafunctionthatacceptsanynumberofarguments,wrapsitinarenderfunctionandtheothernecessarybitsmentionedaboveandregistersitwiththetemplatesystem.
(為了降低customtags的難度,你可以使用Django提供的快捷函數(shù)simple_tag)
Ourcurrent_timefunctioncouldthusbewrittenlikethis:
import datetimefrom django import templateregister = template.Library()# 只有向系統(tǒng)注冊(cè)過(guò)的tags,系統(tǒng)才認(rèn)得你。@register.simple_tag(takes_context=True)#加上這句后我就是一名合格的template tagsdef current_time(context, format_string): timezone = context['timezone'] return your_get_current_time_method(timezone, format_string)
Note that the first argument must be called context.
Note that the first argument must be called context.
Note that the first argument must be called context.
@register.simple_tag(takes_context=True)# 這個(gè)裝飾器表明這個(gè)函數(shù)是一個(gè)模板標(biāo)簽,takes_context = True 表示接收上下文對(duì)象,就是前面所說(shuō)的封裝了各種變量的 Context 對(duì)象。def paginate(context, object_list ,page_count): # context是Context 對(duì)象,object_list是你要分頁(yè)的對(duì)象,page_count表示每頁(yè)的數(shù)量 , page_count left = 3 # 當(dāng)前頁(yè)碼左邊顯示幾個(gè)頁(yè)碼號(hào) -1,比如3就顯示2個(gè) right = 3 # 當(dāng)前頁(yè)碼右邊顯示幾個(gè)頁(yè)碼號(hào) -1 paginator = Paginator(object_list, page_count) # 通過(guò)object_list分頁(yè)對(duì)象 page = context['request'].GET.get('page') # 從 Http 請(qǐng)求中獲取用戶請(qǐng)求的頁(yè)碼號(hào) try: object_list = paginator.page(page) # 根據(jù)頁(yè)碼號(hào)獲取第幾頁(yè)的數(shù)據(jù) context['current_page'] = int(page) # 把當(dāng)前頁(yè)封裝進(jìn)context(上下文)中 pages = get_left(context['current_page'], left, paginator.num_pages) + get_right(context['current_page'], right, paginator.num_pages) # 調(diào)用了兩個(gè)輔助函數(shù),根據(jù)當(dāng)前頁(yè)得到了左右的頁(yè)碼號(hào),比如設(shè)置成獲取左右兩邊2個(gè)頁(yè)碼號(hào),那么假如當(dāng)前頁(yè)是5,則 pages = [3,4,5,6,7],當(dāng)然一些細(xì)節(jié)需要處理,比如如果當(dāng)前頁(yè)是2,那么獲取的是pages = [1,2,3,4] except PageNotAnInteger: # 異常處理,如果用戶傳遞的page值不是整數(shù),則把第一頁(yè)的值返回給他 object_list = paginator.page(1) context['current_page'] = 1 # 當(dāng)前頁(yè)是1 pages = get_right(context['current_page'], right, paginator.num_pages) except EmptyPage: # 如果用戶傳遞的 page 值是一個(gè)空值,那么把最后一頁(yè)的值返回給他 object_list = paginator.page(paginator.num_pages) context['current_page'] = paginator.num_pages # 當(dāng)前頁(yè)是最后一頁(yè),num_pages的值是總分頁(yè)數(shù) pages = get_left(context['current_page'], left, paginator.num_pages) context['article_list'] = object_list # 把獲取到的分頁(yè)的數(shù)據(jù)封裝到上下文中 context['pages'] = pages # 把頁(yè)碼號(hào)列表封裝進(jìn)去 context['last_page'] = paginator.num_pages # 最后一頁(yè)的頁(yè)碼號(hào) context['first_page'] = 1 # 第一頁(yè)的頁(yè)碼號(hào)為1 try: # 獲取 pages 列表第一個(gè)值和最后一個(gè)值,主要用于在是否該插入省略號(hào)的判斷,在模板文件中將會(huì)體會(huì)到它的用處。注意這里可能產(chǎn)生異常,因?yàn)閜ages可能是一個(gè)空列表,比如本身只有一個(gè)分頁(yè),那么pages就為空,因?yàn)槲覀冇肋h(yuǎn)不會(huì)獲取頁(yè)碼為1的頁(yè)碼號(hào)(至少有1頁(yè),1的頁(yè)碼號(hào)已經(jīng)固定寫(xiě)在模板文件中) context['pages_first'] = pages[0] context['pages_last'] = pages[-1] + 1 # +1的原因是為了方便判斷,在模板文件中將會(huì)體會(huì)到其作用。 except IndexError: context['pages_first'] = 1 # 發(fā)生異常說(shuō)明只有1頁(yè) context['pages_last'] = 2 # 1 + 1 后的值 return '' # 必須加這個(gè),否則首頁(yè)會(huì)顯示個(gè)None
新聞熱點(diǎn)
疑難解答
圖片精選