Django:{%循环%}有问题

发布于 2024-08-04 15:49:20 字数 400 浏览 3 评论 0原文

这直接取自我的 Django 模板:

{% for day in days %}
  <!-- {% cycle 'day' 'day' 'day last' as cls %} -->
  {% rounded "black" cls %} {# Custom tag giving me rounded borders. #}
  ...
  {% endrounded %}
{% endfor %}

我注释掉了 {% Cycle %} 因为我只用它来将“cls”设置为“day last””循环中每第三次迭代。有没有更好的方法可以在不向视图添加任何代码的情况下执行此操作? (人们说逻辑应该远离模板,但反之则更糟糕。)

This is taken directly from my Django template:

{% for day in days %}
  <!-- {% cycle 'day' 'day' 'day last' as cls %} -->
  {% rounded "black" cls %} {# Custom tag giving me rounded borders. #}
  ...
  {% endrounded %}
{% endfor %}

I have commented out the {% cycle %} because I only use it to set "cls" to "day last" every third iteration in the loop. Is there any better way to do this without adding any code to the view? (People say that logic should stay out of templates, but having it the other way around is almost worse.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你对谁都笑 2024-08-11 15:49:20

我不确定我是否理解您遇到问题的原因,因为您当前的解决方案似乎有效。我认为您不需要 HTML 注释,因为 {% Cycle %}as 不会输出任何内容,但除此之外它似乎还不错。

但是,如果您想要另一种方法来做到这一点,您可以使用 divisibleby 过滤器:

{% for day in days %}
  {% if forloop.counter|divisibleby:3 %}
     {% rounded "black" "day last" %}
  {% else %}
     {% rounded "black" "day" %}
  {% endif %}
{% endfor %}

但我认为这并不比您已有的更好。

I'm not sure I understand why you have a problem, since your current solution seems to work. I don't think you need the HTML comments, since {% cycle %} with as doesn't output anything, but apart from that it seems fine.

However if you want another way to do it, you could use the divisibleby filter:

{% for day in days %}
  {% if forloop.counter|divisibleby:3 %}
     {% rounded "black" "day last" %}
  {% else %}
     {% rounded "black" "day" %}
  {% endif %}
{% endfor %}

but I don't think this is any better than what you have already.

寒冷纷飞旳雪 2024-08-11 15:49:20

“Django 开发版本中的新增功能”

循环“as”标签现在具有“静默”模式:

http://docs.djangoproject.com/en/dev/ref/templates/builtins/#cycle

http://code.djangoproject.com/ticket/13567

'New in Django Development version'

The cycle 'as' tag now has a 'silent' mode:

http://docs.djangoproject.com/en/dev/ref/templates/builtins/#cycle

http://code.djangoproject.com/ticket/13567

陌伤浅笑 2024-08-11 15:49:20

应用程序逻辑不应该位于您的模板中。表示逻辑确实属于那里(看起来就是这样)。我会把它放在那里。

Application logic shouldn't be in your templates. Presentation logic does belong there (which is what this appears to be). I'd put it there.

小耗子 2024-08-11 15:49:20

嗯...这就是 cycle 标签的用途。我不知道你为什么要试图避免它,但你应该知道:

  1. 任何需要选择性应用某些规则的逻辑都需要 python 代码,因为模板代码不会分配给变量。
  2. python 代码必须从模板标签或视图函数逻辑调用。

因此,如果您不使用视图逻辑,那么您就会陷入模板标签的困境。要么你自己编写,要么使用内置的。cycle 似乎和其他任何东西一样简单。有什么问题。

Ummm...that's what the cycle tag is for. I have no idea why you're trying to avoid it, but you should know:

  1. Any logic requiring selective application of some rule will require python code, since template code doesn't assign to variables.
  2. The python code must be either called from a templatetag or view function logic.

So if you won't use view logic, you're stuck with a templatetags. Either you write your own or you use one that's built in. cycle seems to be about as easy as any other. What's the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文