最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Django中自定義filter并在template中的使用詳解

 更新時(shí)間:2020年05月19日 10:58:27   作者:LiveMost  
這篇文章主要介紹了在Django中自定義filter并在template中的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

Django內(nèi)置的filter有很多,然而我們由于業(yè)務(wù)邏輯的特殊要求,有時(shí)候仍然會(huì)不夠用,這個(gè)時(shí)候就需要我們自定義filter來實(shí)現(xiàn)相應(yīng)的內(nèi)容。接下來讓我們從自定義一個(gè)get_range(value)來產(chǎn)生列表的filter開始吧。

首先在你的django app的models.py的同級(jí)目錄建立一個(gè)templatetags的文件夾,并在里面新建一個(gè)init.py的空文件,這個(gè)文件確保了這個(gè)文件夾被當(dāng)做一個(gè)python的包。在添加了templatetags模塊之后,我們需要重新啟動(dòng)服務(wù)器才能使其有效。

polls/
  __init__.py
  models.py
  templatetags/
    __init__.py
  views.py

然后在templatetags中新建一個(gè)python文件,文件名就是以后需要加載到頁面的自定義庫(kù)的名字。在這里我們新建一個(gè)generalfilters.py文件。

polls/
  __init__.py
  models.py
  templatetags/
    __init__.py
    generalfilters.py
  views.py

為了讓庫(kù)生效,必須在文件里添加一個(gè)模塊級(jí)別的register變量。它是template.Library的實(shí)例,確保了標(biāo)簽和過濾器的有效性。

編輯generalfilters.py,添加

from django import template
register=template.Library()
@register.filter
def get_range(value):
  return range(value)

上述代碼中定義了一個(gè)生成列表的函數(shù),@register.filter表示這個(gè)函數(shù)是一個(gè)過濾器。至此我們的生成列表的過濾器就已經(jīng)寫好了。接下來我們需要把這個(gè)過濾器的庫(kù)加載到模板里。

在你想要使用的模板的頂部加上{% load generalfilters %},就可以使用這個(gè)過濾器了。

{% for i in 5|get_range_bet_within %}
  {{i}}
{% endfor %}

運(yùn)行結(jié)果

補(bǔ)充知識(shí):Django 自定義篩選器:重寫DateFieldListFilter

我就廢話不多說了,大家還是直接看代碼吧!

class MyDateTimeFilter(admin.filters.DateFieldListFilter):
  def __init__(self, *args, **kwargs):
    super(MyDateTimeFilter, self).__init__(*args, **kwargs)
 
    now = timezone.now()
    # When time zone support is enabled, convert "now" to the user's time
    # zone so Django's definition of "Today" matches what the user expects.
    if timezone.is_aware(now):
      now = timezone.localtime(now)
 
    filter_end_date = now.replace(hour=0, minute=0, second=0, microsecond=0)
 
    filter_start_date_for_one_week = filter_end_date - datetime.timedelta(days=7)
 
    month_with_day31 = [1,3,5,7,8,10,12]
    if filter_end_date.month in month_with_day31 and filter_end_date.day == 31 and filter_end_date.month != 3:
      if filter_end_date.month == 1:
        filter_start_date_for_one_month = filter_end_date.replace(year=filter_end_date.year-1, month=12)
      else:
        filter_start_date_for_one_month = filter_end_date.replace(month=filter_end_date.month-1, day=30)
    elif filter_end_date.month == 3 and filter_end_date.day in [29, 30, 31]:
      if is_leap_year(filter_end_date.year):
        filter_start_date_for_one_month = filter_end_date.replace(month=filter_end_date.month-1, day=29)
      else:
        filter_start_date_for_one_month = filter_end_date.replace(month=filter_end_date.month-1, day=28)
    else:
      if filter_end_date.month == 1:
        filter_start_date_for_one_month = filter_end_date.replace(year=filter_end_date.year-1, month=12)
      else:
        filter_start_date_for_one_month = filter_end_date.replace(month=filter_end_date.month-1)
    
    filter_start_date_for_six_month = ''
    filter_start_date_for_six_month_month = (filter_end_date.month - 6 + 12) % 12
    if filter_start_date_for_six_month_month == 0:
      filter_start_date_for_six_month_month = 12
    if filter_start_date_for_six_month_month in month_with_day31:
      if filter_end_date.month > 6:
        filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month)
      else:
        filter_start_date_for_six_month = filter_end_date.replace(year=filter_end_date.year-1, month=filter_start_date_for_six_month_month)
    elif filter_start_date_for_six_month_month == 2:
      if filter_end_date.day in [29, 30, 31]:
        if is_leap_year(filter_end_date.year):
          filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month, day=29)
        else:
          filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month, day=28)
      else:
        filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month)
    else:
      if filter_end_date.day == 31 and filter_end_date.month >6:
        filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month, day=30)
      elif filter_end_date.day == 31 and filter_end_date.month <=6:
        filter_start_date_for_six_month = filter_end_date.replace(year=filter_end_date.year-1, month=filter_start_date_for_six_month_month, day=30)
      elif filter_end_date.day <31 and filter_end_date.month >6:
        filter_start_date_for_six_month = filter_end_date.replace(month=filter_start_date_for_six_month_month)
      else:
        filter_start_date_for_six_month = filter_end_date.replace(year=filter_end_date.year-1, month=filter_start_date_for_six_month_month)
 
    filter_end_date = filter_end_date + datetime.timedelta(days=1)
 
    self.links = ((
      ('------', {}),
      ('Past week', {
        self.lookup_kwarg_since: str(filter_start_date_for_one_week),
        self.lookup_kwarg_until: str(filter_end_date),
      }),
      ('Past month', {
        self.lookup_kwarg_since: str(filter_start_date_for_one_month),
        self.lookup_kwarg_until: str(filter_end_date),
      }),
      ('Past 6 months', {
        self.lookup_kwarg_since: str(filter_start_date_for_six_month),
        self.lookup_kwarg_until: str(filter_end_date),
      }),
      ('All', {}),
    ))

以上這篇在Django中自定義filter并在template中的使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python利用函數(shù)式編程實(shí)現(xiàn)優(yōu)化代碼

    Python利用函數(shù)式編程實(shí)現(xiàn)優(yōu)化代碼

    函數(shù)式編程(Functional Programming)是一種編程范式,它將計(jì)算視為函數(shù)的求值,并且避免使用可變狀態(tài)和循環(huán),在Python中還可以利用它的簡(jiǎn)潔和高效來解決實(shí)際問題,下面我們就來學(xué)習(xí)一下它的具體用法吧
    2023-11-11
  • python中使用時(shí)間戳timestamp問題

    python中使用時(shí)間戳timestamp問題

    文章介紹了Python中使用時(shí)間戳和時(shí)間模塊的操作,包括獲取當(dāng)前時(shí)間、計(jì)算程序運(yùn)行時(shí)間以及時(shí)間戳與時(shí)間字符串之間的轉(zhuǎn)換
    2025-02-02
  • python函數(shù)指定默認(rèn)值的實(shí)例講解

    python函數(shù)指定默認(rèn)值的實(shí)例講解

    在本篇內(nèi)容里小編給大家整理了一篇關(guān)于python函數(shù)指定默認(rèn)值的實(shí)例講解內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。
    2021-03-03
  • Python封裝解構(gòu)以及丟棄變量

    Python封裝解構(gòu)以及丟棄變量

    這篇文章主要介紹了Python封裝解構(gòu)以及丟棄變量,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-09-09
  • Python 3.8 新功能全解

    Python 3.8 新功能全解

    這篇文章主要介紹了Python 3.8 新功能全解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python趣味挑戰(zhàn)之教你用pygame畫進(jìn)度條

    Python趣味挑戰(zhàn)之教你用pygame畫進(jìn)度條

    pygame四種方法教會(huì)你畫進(jìn)度條,其實(shí)也不難,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • 使用python實(shí)現(xiàn)抓取騰訊視頻所有電影的爬蟲

    使用python實(shí)現(xiàn)抓取騰訊視頻所有電影的爬蟲

    這篇文章主要介紹了使用python實(shí)現(xiàn)抓取騰訊視頻所有電影的爬蟲,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-04-04
  • pytorch 共享參數(shù)的示例

    pytorch 共享參數(shù)的示例

    今天小編就為大家分享一篇pytorch 共享參數(shù)的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python實(shí)現(xiàn)前端樣式尺寸單位轉(zhuǎn)換

    Python實(shí)現(xiàn)前端樣式尺寸單位轉(zhuǎn)換

    在?Web?前端項(xiàng)目開發(fā)時(shí),樣式尺寸都是以?rpx?為單位,可是?UI?設(shè)計(jì)師在看完開發(fā)后的?UI?,卻要求都以?px?為單位,所以本文就和大家分享一個(gè)利用Python就能實(shí)現(xiàn)尺寸單位轉(zhuǎn)換的方法吧
    2023-06-06
  • pyqt QGraphicsView 以鼠標(biāo)為中心進(jìn)行縮放功能實(shí)現(xiàn)

    pyqt QGraphicsView 以鼠標(biāo)為中心進(jìn)行縮放功能實(shí)現(xiàn)

    在PyQt開發(fā)中,實(shí)現(xiàn)QGraphicsView的鼠標(biāo)中心縮放功能需要注意初始化以及關(guān)鍵函數(shù)的重定義,遇到不達(dá)預(yù)期的效果時(shí),可能需要重寫所有鼠標(biāo)事件,本文記錄了解決QGraphicsView鼠標(biāo)縮放問題的過程,供開發(fā)者參考
    2024-10-10

最新評(píng)論

濮阳市| 泽库县| 体育| 平和县| 湘阴县| 西城区| 呼和浩特市| 北川| 长汀县| 北京市| 兰州市| 福安市| 绍兴县| 齐齐哈尔市| 紫云| 凤凰县| 江口县| 信宜市| 大石桥市| 拉萨市| 卫辉市| 博罗县| 浑源县| 青田县| 綦江县| 仁怀市| 德保县| 驻马店市| 巴林左旗| 沈丘县| 淮滨县| 讷河市| 西贡区| 高阳县| 温宿县| 湖口县| 和林格尔县| 郁南县| 孝义市| 武宁县| 海林市|