关闭
Hit
enter
to search or
ESC
to close
May I Suggest ?
#leanote #leanote blog #code #hello world
柯仓无居所
Home
Archives
Tags
Search
Django如何使用模板标记分割字符串
? django ?
? 渲染 ?
? 字符串分割 ?
1077
0
0
swimming
? django ?
? 渲染 ?
? 字符串分割 ?
[Django如何使用模板标记分割字符串](https://www.stacknoob.com/s/V4vxJz8Gxj66oFNVeqjDiU) > django 渲染时遇到字符串需要分割,此时需要使用 django 的自定义过滤器,具体操作方法如下: 1. 自定义过滤模板,再项目内新建文件夹**templatetags**,同时新建文件**__init__.py** 2. 创建自定义过滤文件,如**myfilter.py**,其内容如下: ```python from django.template import Library register = Library() @register.filter(name="split") def split(value, key): """ Returns the value turned into a list. """ return value.split(key) ``` 3. 模板中引用方法如下: ```python {% with value|split:"," as details %} {% for p in details %} <td>{{ p }}</td> {% endfor %} {% endwith %} ``` > 此方法,仅仅为分割字符串参考。
觉得不错,点个赞?
提交评论
Sign in
to leave a comment.
No Leanote account ?
Sign up now
.
0
条评论
More...
文章目录
No Leanote account ? Sign up now.