django 自定义filter 判断if var in list的例子
作者:一只小coder 时间:2022-09-25 16:11:37
1. 需求:
用户答题练习,当用户获取所有题目的同时,需要判断用户是否已经做过该题目,如果做过,需要render的时候添加一个“回顾”按钮。
2. 实现
a. 查询用户做过题目的id
finished_objs=UserSpeaking.objects.filter(user=request.user)
finished_ids=[ obj.id for obj in finished_objs]
b. 创建filters.py
from django import template
register = template.Library()
def include_filter(value,values):
return True if value in values else False
register.filter('include', include_filter)
c. 在模板中引用
{% load filters %}
...
{% if test.id|include:finished_ids %}
<a href="{% url 'practice_history' test.id %}" rel="external nofollow" class="go_to_btn lf">去回顾</a>
{% endif %}
来源:https://blog.csdn.net/u014633966/article/details/79004464
标签:django,filter,判断
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用hasOwnProperty给数组去重的面试题分享
2023-08-06 20:48:37
ASP代理采集的核心函数代码
2010-01-02 20:43:00
python使用opencv驱动摄像头的方法
2023-08-26 17:00:49
XML DOM介绍和例子
2007-10-15 20:23:00
python实现简单贪吃蛇小游戏
2021-12-11 13:59:00
Dreaweaver MX 2004新功能:图片处理
2010-09-02 12:38:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113029322s.gif)
也说说语义
2009-12-29 13:04:00
基于Python pyecharts实现多种图例代码解析
2021-11-10 05:20:17
![](https://img.aspxhome.com/file/2023/9/75939_0s.png)
微信小程序 云开发模糊查询实现解析
2023-08-24 14:47:57
Asp 返回引用类型函数代码
2011-03-10 10:44:00
有关JS中Event对象的几点总结
2009-03-06 12:36:00
设计良好网页的4项原则
2009-04-24 12:48:00
![](https://img.aspxhome.com/file/UploadPic/20094/24/01-51s.jpg)
redis不能访问本机真实ip地址的解决方案
2023-07-05 13:09:44
禁用JavaScript脚本来复制网站内容
2007-02-03 11:30:00
Python程序控制语句用法实例分析
2021-04-13 06:59:48
![](https://img.aspxhome.com/file/2023/6/72146_0s.png)
浅析Python 序列化与反序列化
2023-05-01 14:36:16
php 多继承的几种常见实现方法示例
2023-11-22 19:03:19
动态提示的select下拉框
2007-12-02 14:54:00
在Python 字典中一键对应多个值的实例
2023-07-25 23:45:02
php5.3 不支持 session_register() 此函数已启用的解决方法
2023-11-16 01:59:39