官方文档:https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/#simple-tags

stackoverflow demo:http://stackoverflow.com/questions/6451304/django-simple-custom-template-tag-example

我的demo:

# -*- coding:utf-8 -*-
from django import template register = template.Library()
@register.simple_tag
def one_of(target, *matchlist):
return target in matchlist

动机:django模板中in关键字的实现

碰到的问题:自定义的tag模块无法加载,解决方案参考:http://stackoverflow.com/questions/5493776/django-current-tags-is-not-a-valid-tag-library

  即:   

  1. 检查installed_app是否有这个模块

  2. 检查有没有语法错误。 通过python manage.py shell 导入相关的函数(one_of),并执行。

3. 检查该模块的是否在tag实际被引用的模块前面(installed_app里面)。我的问题就卡这里。

4. 其他, 删掉pyc文件、重启服务等等。

使用:xxx.html

  

{% load my_tags %}
{% one_of 'taget', 'aaa','bbb','ccc','ddd', 'target' as is_in %}
{% if is_in %}
xxxx
{% endif %}

  

django tag的更多相关文章

  1. 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...

  2. Django 文章标签功能

    使用第三方框架django-taggit为模型添加标签功能,此模块是一个可复用的应用 首先安装 https://github.com/alex/django-taggit 这是项目主页 pip ins ...

  3. Django QuerySet 进阶

    QuerySet 进阶 阅读本文你可以学习到什么 1. 查看 Django queryset 执行的 SQL(1部分) 2. 获得的查询结果直接以类似list方式展示(2,3 部分) 3. 如何在dj ...

  4. Hive LLAP

    body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...

  5. Kafka 消费组消费者分配策略

    body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...

  6. Flink SQL结合Kafka、Elasticsearch、Kibana实时分析电商用户行为

    body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...

  7. Hadoop源码:namenode格式化和启动过程实现

    body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...

  8. Hadoop hdfs副本存储和纠删码(Erasure Coding)存储优缺点

    body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...

  9. Java程序中的代理作用和应用场景及实现

    body { margin: 0 auto; font: 13px / 1 Helvetica, Arial, sans-serif; color: rgba(68, 68, 68, 1); padd ...

随机推荐

  1. Activity的生命周期

    Activity的生命周期 以往我们实现页面间的跳转都是实例化Intent类的对象,但是页面在我们眼前的出现与消失没有我们所看到的那么简单,它有一个复杂的生命周期,一个页面的出现,被覆盖,再次出现,被 ...

  2. JavaScript模板引擎artTemplate.js——template.helper()方法

    上一篇文章我们已经讲到了helper()方法,但是上面的例子只是一个参数的写法,如果是多个参数,写法就另有区别了. <div id="user_info"></d ...

  3. C++知识回顾(二)——名称空间

    随着工程的增大,名称相互冲突的肯能性增加,使用多个厂商的类库时,可能导致名称冲突.为了解决这些问题,需要用到名称空间. 为了将代码加入名称空间,可以使用namespae将其包含,例如,编写一段代码,其 ...

  4. UDP Server

    //UDP服务器端程序,可以接受广播,不可接受多播,多播需要join播地址@Override public void run() { while (true) { try { DatagramSock ...

  5. java 导出word 并下载

    记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...

  6. ubuntu中pycharm安装激活第二种方法的密钥

    43B4A73YYJ-eyJsaWNlbnNlSWQiOiI0M0I0QTczWVlKIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYXNza WduZWVOYW1lIjoi ...

  7. Android 横竖屏+碎片的应用

    最终效果展示: 项目介绍: 通过碎片的方式显示标题列表和内容,其中也牵涉到横竖屏的知识 项目代码下载:http://files.cnblogs.com/files/Laopengblog/%E7%A2 ...

  8. Nginx配置SSL证书部署HTTPS网站

    1.购买ssl证书 购买网站:沃通 2.上传证书到nginx服务器,然后进行解压. 解压后的的效果: [root@bubidev-ng3 nginx]# pwd/etc/nginx [root@bub ...

  9. PHP 进程间通信——消息队列(msg_queue)

    PHP 进程间通信--消息队列 本文不涉及PHP基础库安装.详细安装说明,请参考官网,或期待后续博客分享. 1.消息队列函数准备 <?php//生成一个消息队列的key$msg_key = ft ...

  10. C#之接口

    接口类Interface1.cs using System; using System.Collections.Generic; using System.Linq; using System.Tex ...