zoukankan      html  css  js  c++  java
  • Django的Template不支持range()函数的问题解决办法

    Exception Type:    TemplateSyntaxError
    Exception Value:   Could not parse the remainder: '(count)' from 'range(count)
    Invalid filter: 'get_range' Exception Location: C:Pythonpython3python37libsite-packagesdjango	emplatease.py in find_filter, line 569 Python Executable: C:Pythonpython3python37python.exe

    就是上面的错误,原因是应为Django不支持range函数,所以我们要自己想办法

    首先在view.py里导入register模块,这是干嘛的呢?他是Django自定义函数的

    from django.template.defaulttags import register

    然后加入你的自定义的函数,科学叫法叫过滤器

    @register.filter
    def get_range(value):
        return range(value)

    然后在html中加入过滤器就好了

    {% for _ in count|get_range %}
         <option>{{ forloop.counter }}</option>
    {% endfor %}
  • 相关阅读:
    Struts2框架
    读者写者问题
    哲学家就餐问题
    理解中断
    理解处理机调度
    理解死锁
    理解进程
    Linux CentOS 6.7 挂载U盘
    家庭-养老院模型理解IOC和DI
    Bash基础
  • 原文地址:https://www.cnblogs.com/ShineLeem/p/11385126.html
Copyright © 2011-2022 走看看