Django模板语言中的自定义方法filter过滤器实现web网页的瀑布流

模板语言自定义方法介绍

自定义方法注意事项

Django中有simple_tag 和 filter 两种自定义方法,之前也提到过,需要注意的是

  1. 扩展目录名称必须是templatetags
  2. templatetags中的自定义标签和过滤器必须依赖于一个django app,也就是说,自定义标签和过滤器是绑定app的。该app应该包含一个templatetags目录,这个目录一个和model.py,views.py在同一个层级
  3. 包含templatetags目录的app一定要在INSTALLED_APPS列表里面
  4. 在页面中使用{% load %}加载扩展方法,load的是模块名,而不是app名
  5. 记得使用 from django import template ,register=template.Library()注册

simple_tag 和 filter 的区别

  • simple_tag:可以有多个参数,一般是做数据处理,但不能做if判断语句
  • filter:一般只能有1个参数(可以字符串切割,变通为多个参数),过滤器,一般是return true或者false,可以和if判断语句使用.在过滤器 {{ var|foo:"bar" }} 中 ,过滤器 foo 会被传入变量 var 和默认参数 bar。过滤器函数应该总有返回值

使用方法

目录结构

目录结构

xx.py代码:

#需要从django中导入一些模块
from django import template
from django.utils.safestring import mark_safe from django.template.base import resolve_variable, Node, TemplateSyntaxError register = template.Library() @register.filter def detail1(value,arg): """ 查看余数是否等于remainder arg="1,2" :param counter: :param allcount: :param remainder: :return: """ allcount, remainder = arg.split(',') allcount = int(allcount) remainder = int(remainder) if value%allcount == remainder: return True return False @register.simple_tag def my_simple_time(v1,v2,v3): return v1 + v2 + v3

simple_tag 页面使用

{% load xx %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% my_simple_time 1 2 3 %} </body> </html>

filter 页面使用

{% load xx %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {{ 23|detail4:"4,0" }} {% if 23|detail4:"4,0" %} <h1>123</h1> {% endif %} </body> </html>

页面实现瀑布流

布局介绍

由于图片有大有小,所以为了页面的整齐,没有空隙,可以将页面body主体分为4列,每一列依次往下排列图片和介绍即可.

由于页面是从左到右排序的,所以我们可以将图片的列队索引+1后除以4后得到的余数,就是所在页面中所在的列数,所以可以使用python中的div()函数来处理.

代码

扩展方法代码:xx.py 文件

from django import template
from django.utils.safestring import mark_safe
from django.template.base import resolve_variable, Node, TemplateSyntaxError register = template.Library() @register.filter def detail1(value,arg): """ 查看余数是否等于remainder arg="1,2" :param counter: :param allcount: :param remainder: :return: """ allcount, remainder = arg.split(',') allcount = int(allcount) remainder = int(remainder) if value%allcount == remainder: return True return False

views.py范例

from django.shortcuts import render# Create your views here.def student(request): img_list = [ {'src': '1.jpg', 'title': 'asdfasdfasdf','content': 'asdf'},# 1 {'src': '2.jpg', 'title': 'asdfasdfasdf','content': 'asdf'},# 2 {'src': '3.jpg', 'title': 'asdfasdfasdf','content': 'asdf'}, {'src': '4.jpg', 'title': 'asdfasdfasdf','content': 'asdf'}, {'src': '18.jpg', 'title': 'asdfasdfasdf','content': 'asdf'},# 5 {'src': '21.jpg', 'title': 'asdfasdfasdf','content': 'asdf'}, ] return render(request, 'student.html', {"img_list":img_list})

html页面代码

{% load xx %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style> .container{ width: 980px; margin: 0 auto; } .container .column{ float: left; width: 245px; } .container .item img{ width: 245px; } </style> </head> <body> <div class="container"> <div class="column"> {% for i in img_list %} {% if forloop.counter|detail1:"4,1" %} <div class="item"> {{ forloop.counter }} <img src="/static/{{ i.src }}"> </div> {% endif %} {% endfor %} </div> <div class="column"> {% for i in img_list %} {% if forloop.counter|detail1:"4,2" %} <div class="item"> {{ forloop.counter }} <img src="/static/{{ i.src }}"> </div> {% endif %} {% endfor %} </div> <div class="column"> {% for i in img_list %} {% if forloop.counter|detail1:"4,3" %} <div class="item"> {{ forloop.counter }} <img src="/static/{{ i.src }}"> </div> {% endif %} {% endfor %} </div> <div class="column"> {% for i in img_list %} {% if forloop.counter|detail1:"4,0" %} <div class="item"> {{ forloop.counter }} <img src="/static/{{ i.src }}"> </div> {% endif %} {% endfor %} </div> </div> </body> </html>

转载于:https://www.cnblogs.com/zxmbky/p/9769153.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279209.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

dsp怪胎_2012年6月最佳怪胎文章

dsp怪胎This past month we covered topics such as why you only have to wipe a disk once to erase it, what RSS is and how you can benefit from using it, how websites are tracking you online, and more. Join us as we look back at the best articles for June. 在…

mysql 回退查询_MYSQL数据库表排序规则不一致导致联表查询,索引不起作用问题...

Mysql数据库表排序规则不一致导致联表查询&#xff0c;索引不起作用问题表更描述: 将mysql数据库中的worktask表添加ishaspic字段。具体操作&#xff1a;(1)数据库worktask表新添是否有图片字段ishaspic&#xff1b;新添字段时&#xff0c;报错[SQL] alter table WorkTask add …

如何在Ubuntu上查看和写入系统日志文件

Linux logs a large amount of events to the disk, where they’re mostly stored in the /var/log directory in plain text. Most log entries go through the system logging daemon, syslogd, and are written to the system log. Linux将大量事件记录到磁盘上&#xff0c…

[转]table中设置tr行间距

原文地址:https://blog.csdn.net/itmyhome1990/article/details/50475616 CSS border-collapse 属性设置表格的边框是否被合并为一个单一的边框 值描述separate默认值。边框会被分开。不会忽略 border-spacing 和 empty-cells 属性。collapse如果可能&#xff0c;边框会合并为一…

向Ubuntu提供反馈的5种方法

Ubuntu, like many other Linux distributions, is a community-developed operating system. In addition to getting involved and submitting patches, there are a variety of ways you can provide useful feedback and suggest features to Ubuntu. 与许多其他Linux发行版…

Tomcat 发布项目 conf/Catalina/localhost 配置 及数据源配置

本文介绍通过在tomcat的conf/Catalina/localhost目录下添加配置文件&#xff0c;来发布项目。因为这样对 tomcat 的入侵性最小&#xff0c;只需要新增一个配置文件&#xff0c;不需要修改原有配置&#xff1b;而且支持动态解析&#xff0c;修改完代码直接生效(修改配置除外)。在…

Centos7 中文乱码

1. 安装中文库 yum groupinstall "fonts" 2. 检查是否有中文语言包 locale -a 3. 查看当前系统语言环境 locale 解析如下 LANG:当前系统的语言LC_CTYPE&#xff1a;语言符号及其分类LC_NUMERIC&#xff1a;数字LC_COLLATE&#xff1a;比较和排序习惯LC_TIME&#xff…

pkpm板按弹性计算还是塑性_双向板按弹性方法还是按塑性方法计算

双向板按弹性方法还是按塑性方法计算茅老师您好&#xff01;想请教您个问题&#xff0c;PKPM计算双向板时一般都是按弹性算吧&#xff0c;可我去年刚进设计院的时候有一个项目是按塑性算的&#xff0c;这样影响大不大啊&#xff0c;支座与跨中弯矩比值系数取得默认的1.8&#x…

chrome自动退出的原因_Chrome 70将让用户选择退出新的自动登录功能

chrome自动退出的原因An upcoming Chrome option allows users to log into Google accounts without logging into the browser. The change was prompted by a backlash among users and privacy advocates. 即将推出的Chrome选项允许用户无需登录浏览器即可登录Google帐户。…

学习笔记DL007:Moore-Penrose伪逆,迹运算,行列式,主成分分析PCA

2019独角兽企业重金招聘Python工程师标准>>> Moore-Penrose伪逆(pseudoinverse)。 非方矩阵&#xff0c;逆矩阵没有定义。矩阵A的左逆B求解线性方程Axy。两边左乘左逆B&#xff0c;xBy。可能无法设计唯一映射将A映射到B。矩阵A行数大于列数&#xff0c;方程无解。矩…

mysql40题_mysql40题

一、表关系请创建如下表&#xff0c;并创建相关约束导入现有数据库数据&#xff1a;/*Navicat Premium Data TransferSource Server : localhostSource Server Type : MySQLSource Server Version :50624Source Host : localhostSource Database : sqlexamTarget Server Type :…

ubuntu取消主目录加密_如何在Ubuntu上恢复加密的主目录

ubuntu取消主目录加密Access an encrypted home directory when you’re not logged in – say, from a live CD – and all you’ll see is a README file. You’ll need a terminal command to recover your encrypted files. 当您未登录时(例如&#xff0c;从实时CD)访问加密…

select 和epoll模型区别

1.select 和epoll模型区别 1.1.网络IO模型概述 通常来说&#xff0c;网络IO可以抽象成用户态和内核态之间的数据交换。一次网络数据读取操作&#xff08;read&#xff09;&#xff0c;可以拆分成两个步骤&#xff1a;1&#xff09;网卡驱动等待数据准备好&#xff08;内核态&…

python数据结构与算法第六讲_Python 学习 -- 数据结构与算法 (六)

栈 是一种 “操作受限”的线性表&#xff0c;只允许在一端插入和删除数据。从功能是上来说&#xff0c;数组和链表确实可以替代栈&#xff0c;但是特定的数据结构是对特定场景的抽象&#xff0c;而且&#xff0c;数组或链表暴露了太多的操作接口&#xff0c;操作上的确灵活自由…

spring-springmvc code-based

idea设置maven在下载依赖的同时把对应的源码下载过来。图0&#xff1a;1主要实现零配置来完成springMVC环境搭建&#xff0c;当然现在有了springBoot也是零配置&#xff0c;但是很多同仁都是从spring3.x中的springMVC直接过渡到springBoot的&#xff0c;spring3.x的MVC大部分都…

powershell 入门_使用PowerShell入门的5个Cmdlet

powershell 入门PowerShell is quickly becoming the preferred scripting language and CLI of Power Users as well as IT Pros. It’s well worth learning a few commands to get you started, so we’ve got 5 useful cmdlets for you to learn today. PowerShellSwift成为…

Part 3: Services

介绍 在第3部分中&#xff0c;我们将扩展应用程序并启用负载平衡。为此&#xff0c;我们必须在分布式应用程序的层次结构中提升一个级别:服务。 StackServices (你在这里)Container (涵盖在第2部分中)关于服务 在分布式应用程序中&#xff0c;应用程序的不同部分被称为“服务”…

mysql ldf文件太大_Linux_数据库清除日志文件(LDF文件过大),清除日志: 复制代码 代码如 - phpStudy...

数据库清除日志文件(LDF文件过大)清除日志&#xff1a;复制代码 代码如下:DECLARE LogicalFileName sysname,MaxMinutes INT,NewSize INTUSE szwzcheck -- 要操作的数据库名SELECT LogicalFileName szwzcheck_Log, -- 日志文件名MaxMinutes 10, -- Limit on time allowed to …

emwin之错误使用控件函数导致死机现象

2018-10-15 导致死机的代码示例如下 1 /**2 * brief widget ID define3 * {4 */5 6 #define ID_WINDOW_0 (GUI_ID_USER 0x00)7 #define ID_TEXT_0 (GUI_ID_USER 0x01)8 #define ID_TEXT_1 (GUI_ID_USER …

diy感应usb摄像头拍照_DIY无线感应充电器

diy感应usb摄像头拍照Courtesy of Instructables user Inducktion shares a very detailed tutorial on how to build a wireless power charger. He explains the impetus behind the project: 由Instructables用户提供Inducktion分享了有关如何构建无线电源充电器的非常详细…