django 自定义过滤器(filter)处理较为复杂的变量方法

yipeiwu_com6年前Python基础

简述:django 在views中有数据需要通过字典(dict)的方式传递给template,该字典中又包含了字典,而且字典中的键值还是一个对象,在template中处理传递过来的数据的时候,字典不能通过键值的方式取出原有数据,对象不能通过(.)的方式直接取出数据,通过大量地查阅资料,最终通过过滤器(filter)的方式解决!

1、需要传递到template的数据,在 views.py 中的index函数中

latest_article_list 是一个Article对象的列表,包含文章ID、作者、发布时间、分类等各种信息

dic['tag_list'] 为一个列表(文章标签列表)

articles_info是一个以字典为元素的列表,而且该字典中 键'article'对应的不是普通变量,而是一个Article对象

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传递过来的变量值?

在index.html中,可以先遍历列表,得到每一个字典变量;

 {% for article_info in articles_info %}

遍历 articles_info 之后的article_info 为一个字典,通过前面的views可以知道里面包含了一个article对象和一个tag_list列表;

对于article_info这个字典变量,在模板中却不能通过键值对获取对应的值,更别说获取Article对象中ID、作者、发布时间等属性值了,为了解决这一问题,这里就需要过滤器才能实现;

3、自定义过滤器

1)、在app目录下建立templagetags文件夹,在此目录下建立空文件 __init__.py和过滤器文件articleinfo.py;

2)、编辑 articleinfo.py,添加过滤器 get_key 和get_attr,get_key获取字典不同键对应的值,get_attr获取Article对象中不同字段对应的值;

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、模板中使用过滤器,获取各种变量值;

index.html中,首先需要通过标签加载上面定义的过滤器文件 articleinfo.py,然后就是index.html模板中调用过滤器了,具体的使用方法见下面的index.html文件;

{% load articleinfo %}

下面的index.html中变量使用的部分代码,使用了双重过滤器提取出了所需要的变量;

比如第4行中

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

首先通过 article_info|get_key:"article" 获取到字典中的article对象,但此处需要的是article对象中的ID属性,由于并不能通过{{ article_info|get_key:"article".id }} 获取到对应的ID值,所以只好双重过滤器来实现了。

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">
        标签:
        {% 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 自定义过滤器(filter)处理较为复杂的变量方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解python函数传参是传值还是传引用

详解python函数传参是传值还是传引用

首先还是应该科普下函数参数传递机制,传值和传引用是什么意思? 函数参数传递机制问题在本质上是调用函数(过程)和被调用函数(过程)在调用发生时进行通信的方法问题。基本的参数传递机制有两种:...

Python编程中的文件读写及相关的文件对象方法讲解

python文件读写 python 进行文件读写的内建函数是open或file file_hander(文件句柄或者叫做对象)= open(filename,mode) mode: 模式...

python简单实现刷新智联简历

python来写一个试试吧,这里使用了cPAMIE模块,代码如下: 代码 from cPAMIE import PAMIE ie=PAMIE("www.zhaopin.com"...

python多行字符串拼接使用小括号的方法

多行字符串拼接使用小括号 s = ('select *' 'from atable' 'where id=888') print s, type(s) #输出 select...

python中定义结构体的方法

Python中没有专门定义结构体的方法,但可以使用class标记定义类来代替结构体,其成员可以在构造函数__init__中定义,具体方法如下。 复制代码 代码如下:class item:...