V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
shooter
V2EX  ›  Python

怎样在 diango 的 template 进行计算呢

  •  
  •   shooter ·
    rebase-network · 2015-12-22 16:27:54 +08:00 · 2851 次点击
    这是一个创建于 3020 天前的主题,其中的信息可能已经有所发展或是发生改变。
    {{ info.number * info.price }}
    
      {% if info.status == 'ERROR' || info.status == 'FAIL' %}
        <span class="label label-danger">{% trans 'FAIL' %}</span>
      {% endif %}
    

    跪求 怎么在 diango 的 template 进行 加减乘除 还有执行 || 呢?

    18 条回复    2015-12-23 21:30:29 +08:00
    cxbig
        1
    cxbig  
       2015-12-22 16:39:03 +08:00
    理论上讲, tpl 只处理简单逻辑,计算的工作提取数据的时候就做好。
    restran
        2
    restran  
       2015-12-22 16:51:11 +08:00
    用 jinja2
    jmc891205
        3
    jmc891205  
       2015-12-22 16:53:02 +08:00
    || 可以用 or: {% if foo1 or foo2}
    FrankFang128
        4
    FrankFang128  
       2015-12-22 16:53:36 +08:00
    kidblg
        5
    kidblg  
       2015-12-22 17:01:16 +08:00
    用 tags 。比如{{ info.number|add:info.price }} 啥的。

    https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/

    不过跟 1 楼一样,还是建议在 views 里处理逻辑,不要让 template 变得臃肿,毕竟它是主要负责渲染,不是处理逻辑。
    tiant167
        6
    tiant167  
       2015-12-22 17:01:55 +08:00
    django 的设计觉得计算之类的复杂逻辑应该放到 view 里算。模版只是渲染用。如果想在模版里计算就用 jinjia 吧
    shooter
        7
    shooter  
    OP
       2015-12-22 17:06:13 +08:00
    @FrankFang128 是啥么意思

    @jmc891205 哦耶, get it

    @restran 这是是 模板引擎的节奏么 臣妾做不了主啊 :cry:

    @cxbig `只处理简单逻辑`, 我感觉 进行一些计算是 简单的逻辑啊 :cry:
    我的使用姿势不对么, 囧
    felixzhu
        8
    felixzhu  
       2015-12-22 17:13:34 +08:00
    强烈建议替换成 Jinja2
    django 的设计原则之一就是 template 只做展示
    shooter
        9
    shooter  
    OP
       2015-12-22 17:13:41 +08:00
    @tiant167 @kidblg

    我觉得这是 django 的一套理念吧, 严格的 mvc 架构.
    template 只能负责渲染, 但是貌似原生 dj 也有一些 `if else for` .
    这个粒度的 不好把控呢.
    出现 `jinjia` 也是为了打破局限性吧
    neoblackcap
        10
    neoblackcap  
       2015-12-22 17:14:16 +08:00
    @shooter 模板简直应该只复杂渲染,减少副作用
    tiant167
        11
    tiant167  
       2015-12-22 17:33:46 +08:00
    @shooter 每个人对模版的理解和想法不太一样~选择一个自己喜欢的就好~ Django 的模版是**想**设计给写界面( HTML )的人使用,而不是写业务代码的后端程序员~ 现在 Django 加了对 jinjia 的支持也是因为大家对这块都有争议和自己的想法啦
    crayonyi
        12
    crayonyi  
       2015-12-22 17:46:16 +08:00
    用自定义的 tag ,或者改用 jinjia 模板
    idblife
        13
    idblife  
       2015-12-22 18:04:56 +08:00
    不建议在模板里进行计算
    latyas
        14
    latyas  
       2015-12-23 02:14:56 +08:00
    django 的模板略坑, QJ 程序员要符合它的 MVT 设计理念,然而会有程序员愿意被 QJ 么?建议换 jinja2
    shooter
        15
    shooter  
    OP
       2015-12-23 10:28:59 +08:00
    https://github.com/dbrgn/django-mathfilters

    django-mathfilters 就是 tag 的思路
    thx
    shooter
        16
    shooter  
    OP
       2015-12-23 10:49:03 +08:00
    人生苦短, 学习 python.
    我感觉 django 给这个说法 -1, py 粉不要削我, 我只是工具使用者. #赶紧逃#
    dododada
        17
    dododada  
       2015-12-23 15:31:31 +08:00
    可以在 view 中写个 filter , template 中的使用方法和 tags 类似
    iwillhappy1314
        18
    iwillhappy1314  
       2015-12-23 21:30:29 +08:00
    有个包: django-mathfilters
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5051 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 09:35 · PVG 17:35 · LAX 02:35 · JFK 05:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.