欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

django 自定義過(guò)濾器(filter)處理較為復(fù)雜的變量方法

 更新時(shí)間:2019年08月12日 10:31:05   作者:Justbreaking  
今天小編就為大家分享一篇django 自定義過(guò)濾器(filter)處理較為復(fù)雜的變量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

簡(jiǎn)述:django 在views中有數(shù)據(jù)需要通過(guò)字典(dict)的方式傳遞給template,該字典中又包含了字典,而且字典中的鍵值還是一個(gè)對(duì)象,在template中處理傳遞過(guò)來(lái)的數(shù)據(jù)的時(shí)候,字典不能通過(guò)鍵值的方式取出原有數(shù)據(jù),對(duì)象不能通過(guò)(.)的方式直接取出數(shù)據(jù),通過(guò)大量地查閱資料,最終通過(guò)過(guò)濾器(filter)的方式解決!

1、需要傳遞到template的數(shù)據(jù),在 views.py 中的index函數(shù)中

latest_article_list 是一個(gè)Article對(duì)象的列表,包含文章ID、作者、發(fā)布時(shí)間、分類等各種信息

dic['tag_list'] 為一個(gè)列表(文章標(biāo)簽列表)

articles_info是一個(gè)以字典為元素的列表,而且該字典中 鍵'article'對(duì)應(yīng)的不是普通變量,而是一個(gè)Article對(duì)象

view.py

def index(request):
  latest_article_list = Article.objects.query_by_time()
  articles_info = []
  dic = {}
  for article in latest_article_list:
    taginfo = Article.objects.get(id=article.id)
    dic['tag_list'] = taginfo.tags.all()
    dic['article'] = article;
    articles_info.append(dic)
    dic = {}

  loginform = LoginForm()
  context = {'articles_info':articles_info, 'loginform':loginform}
  return render(request, 'index.html', context)

2、template如何引用views傳遞過(guò)來(lái)的變量值?

在index.html中,可以先遍歷列表,得到每一個(gè)字典變量;

 {% for article_info in articles_info %}

遍歷 articles_info 之后的article_info 為一個(gè)字典,通過(guò)前面的views可以知道里面包含了一個(gè)article對(duì)象和一個(gè)tag_list列表;

對(duì)于article_info這個(gè)字典變量,在模板中卻不能通過(guò)鍵值對(duì)獲取對(duì)應(yīng)的值,更別說(shuō)獲取Article對(duì)象中ID、作者、發(fā)布時(shí)間等屬性值了,為了解決這一問題,這里就需要過(guò)濾器才能實(shí)現(xiàn);

3、自定義過(guò)濾器

1)、在app目錄下建立templagetags文件夾,在此目錄下建立空文件 __init__.py和過(guò)濾器文件articleinfo.py;

2)、編輯 articleinfo.py,添加過(guò)濾器 get_key 和get_attr,get_key獲取字典不同鍵對(duì)應(yīng)的值,get_attr獲取Article對(duì)象中不同字段對(duì)應(yīng)的值;

articleinfo.py

from django import template
register = template.Library()

@register.filter
def get_key(d, key_name):
  return d.get(key_name)

@register.filter
def get_attr(d, m):
  if hasattr(d, m):
    return getattr(d, m)

4、模板中使用過(guò)濾器,獲取各種變量值;

index.html中,首先需要通過(guò)標(biāo)簽加載上面定義的過(guò)濾器文件 articleinfo.py,然后就是index.html模板中調(diào)用過(guò)濾器了,具體的使用方法見下面的index.html文件;

{% load articleinfo %}

下面的index.html中變量使用的部分代碼,使用了雙重過(guò)濾器提取出了所需要的變量;

比如第4行中

{{ article_info|get_key:"article"|get_attr:"id" }}

首先通過(guò) article_info|get_key:"article" 獲取到字典中的article對(duì)象,但此處需要的是article對(duì)象中的ID屬性,由于并不能通過(guò){{ article_info|get_key:"article".id }} 獲取到對(duì)應(yīng)的ID值,所以只好雙重過(guò)濾器來(lái)實(shí)現(xiàn)了。

index.html

{% for article_info in articles_info %}
  <div class="row">
    <article class="col-xs-12">
      <h3><a id="article_title", href="/focus/{{ article_info|get_key:" rel="external nofollow" article"|get_attr:"id" }}">{{ article_info|get_key:"article"|get_attr:"title" }}</a></h3>
      <div class="article_info">
        <span class="">{{ article_info|get_key:"article"|get_attr:"author" }}</span>
        <span class="">{{ article_info|get_key:"article"|get_attr:"create_time"|date:"Y-m-d H:i" }}</span>
      </div>
      <div class="category">
        分類:
         <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class>{{ article_info|get_key:"article"|get_attr:"category" }}</a>
      </div>
      <div class="category">
        標(biāo)簽:
        {% for tag in article_info|get_key:"tag_list" %}
          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ tag }}</a>
        {% endfor %}
      </div>
      <p>{{ article_info|get_key:"article"|get_attr:"content"|truncatechars_html:80 | safe }}</p>
      <p><button class="btn btn-default" onclick="window.location.href='/focus/{{ article_info|get_key:"article"|get_attr:"id" }}' ">Read More</button></p>
      <ul class="list-inline">
        <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span class="glyphicon glyphicon-comment"></span>{{ article_info|get_key:"article"|get_attr:"comment_num" }} Comments</a></li>
        <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><span class="glyphicon glyphicon-thumbs-up"></span>{{ article_info|get_key:"article"|get_attr:"like_num" }} Likes</a></li>
      </ul>
    </article>
  </div>      
  <hr>
{% endfor %}

以上這篇django 自定義過(guò)濾器(filter)處理較為復(fù)雜的變量方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django REST framework 單元測(cè)試實(shí)例解析

    Django REST framework 單元測(cè)試實(shí)例解析

    這篇文章主要介紹了Django REST framework 單元測(cè)試實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python pkg_resources模塊動(dòng)態(tài)加載插件實(shí)例分析

    Python pkg_resources模塊動(dòng)態(tài)加載插件實(shí)例分析

    當(dāng)編寫應(yīng)用軟件時(shí),我們通常希望程序具有一定的擴(kuò)展性,額外的功能——甚至所有非核心的功能,都能通過(guò)插件實(shí)現(xiàn),具有可插拔性。特別是使用 Python 編寫的程序,由于語(yǔ)言本身的動(dòng)態(tài)特性,為我們的插件方案提供了很多種實(shí)現(xiàn)方式
    2022-08-08
  • 深入探討Python中的內(nèi)置類屬性`__repr__`

    深入探討Python中的內(nèi)置類屬性`__repr__`

    在Python中,__repr__是一個(gè)特殊的內(nèi)置類屬性,用于定義類的字符串表示形式,本文將深入探討__repr__的作用、用法以及一些實(shí)際應(yīng)用場(chǎng)景,希望對(duì)大家有所幫助
    2023-12-12
  • Python繪圖系統(tǒng)之自定義一個(gè)坐標(biāo)列表控件

    Python繪圖系統(tǒng)之自定義一個(gè)坐標(biāo)列表控件

    這篇文章主要為大家詳細(xì)介紹了Python如何編寫一個(gè)繪圖系統(tǒng),可以實(shí)現(xiàn)自定義一個(gè)坐標(biāo)列表控件,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2023-08-08
  • python dict.get()和dict[''key'']的區(qū)別詳解

    python dict.get()和dict[''key'']的區(qū)別詳解

    下面小編就為大家?guī)?lái)一篇python dict.get()和dict['key']的區(qū)別詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • python模擬登錄百度貼吧(百度貼吧登錄)實(shí)例

    python模擬登錄百度貼吧(百度貼吧登錄)實(shí)例

    python模擬登錄百度貼吧實(shí)例分享,大家參考使用吧
    2013-12-12
  • Pycharm使用爬蟲時(shí)遇到etree紅線問題及解決

    Pycharm使用爬蟲時(shí)遇到etree紅線問題及解決

    這篇文章主要介紹了Pycharm使用爬蟲時(shí)遇到etree紅線問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python 3.7.4 安裝 opencv的教程

    python 3.7.4 安裝 opencv的教程

    這篇文章主要介紹了python 3.7.4 安裝 opencv的教程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • python如何正確的操作字符串

    python如何正確的操作字符串

    Python是一種知道如何不妨礙你編寫程序的編程語(yǔ)言。它易于學(xué)習(xí),功能強(qiáng)大,足以構(gòu)建Web應(yīng)用程序并自動(dòng)化無(wú)聊的東西。本文是對(duì)常用字符串操作進(jìn)行了詳細(xì)的總結(jié)分析,希望對(duì)您有所幫助。
    2021-06-06
  • 淺談Python中的異常和JSON讀寫數(shù)據(jù)的實(shí)現(xiàn)

    淺談Python中的異常和JSON讀寫數(shù)據(jù)的實(shí)現(xiàn)

    今天小編就為大家分享一篇淺談Python中的異常和JSON讀寫數(shù)據(jù)的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02

最新評(píng)論