V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Dec 22, 2015 · 3869 views
    This topic created in 3779 days ago, the information mentioned may be changed or developed.
    {{ info.number * info.price }}
    
      {% if info.status == 'ERROR' || info.status == 'FAIL' %}
        <span class="label label-danger">{% trans 'FAIL' %}</span>
      {% endif %}
    

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

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

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

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

    @jmc891205 哦耶, get it

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

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

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

    django-mathfilters 就是 tag 的思路
    thx
    shooter
        16
    shooter  
    OP
       Dec 23, 2015
    人生苦短, 学习 python.
    我感觉 django 给这个说法 -1, py 粉不要削我, 我只是工具使用者. #赶紧逃#
    dododada
        17
    dododada  
       Dec 23, 2015
    可以在 view 中写个 filter , template 中的使用方法和 tags 类似
    iwillhappy1314
        18
    iwillhappy1314  
       Dec 23, 2015
    有个包: django-mathfilters
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1275 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 23:38 · PVG 07:38 · LAX 16:38 · JFK 19:38
    ♥ Do have faith in what you're doing.